strconv

package
v1.21.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

パッケージ strconv は基本データ型の文字列表現への変換を実装します。

数値の変換

最も一般的な数値の変換は Atoi (文字列から整数へ) と Itoa (整数から文字列へ) です。

i, err := strconv.Atoi("-42")
s := strconv.Itoa(-42)

これらは10進数とGoのint型を仮定しています。

ParseBoolParseFloatParseInt、および ParseUint は文字列を値に変換します:

b, err := strconv.ParseBool("true")
f, err := strconv.ParseFloat("3.1415", 64)
i, err := strconv.ParseInt("-42", 10, 64)
u, err := strconv.ParseUint("42", 10, 64)

パース関数は最も広い型(float64、int64、およびuint64)を返しますが、サイズ引数が より狭い幅を指定している場合、結果はその狭い型にデータの損失なく変換できます:

s := "2147483647" // 最大のint32
i64, err := strconv.ParseInt(s, 10, 32)
...
i := int32(i64)

FormatBoolFormatFloatFormatInt、および FormatUint は値を文字列に変換します:

s := strconv.FormatBool(true)
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
s := strconv.FormatInt(-42, 16)
s := strconv.FormatUint(42, 16)

AppendBoolAppendFloatAppendInt、および AppendUint は類似していますが、 フォーマットされた値を宛先スライスに追加します。

文字列の変換

Quote および QuoteToASCII は文字列をクォートされたGo文字リテラルに変換します。 後者は非ASCII Unicodeを \u でエスケープして、結果がASCII文字列であることを保証します:

q := strconv.Quote("Hello, 世界")
q := strconv.QuoteToASCII("Hello, 世界")

QuoteRune および QuoteRuneToASCII は類似していますが、runeを受け入れて、 クォートされたGo runeリテラルを返します。

Unquote および UnquoteChar はGo文字列およびruneリテラルのクォートを解除します。

Index

Examples

Constants

View Source
const IntSize = intSize

IntSizeはintまたはuint値のビットサイズです。

Variables

View Source
var ErrRange = errors.New("value out of range")

ErrRangeは、値が対象の型の範囲外であることを示します。

View Source
var ErrSyntax = errors.New("invalid syntax")

ErrSyntaxは、値がターゲットの型の正しい構文ではないことを示します。

Functions

func AppendBool

func AppendBool(dst []byte, b bool) []byte

AppendBoolはbの値に応じて、"true"または"false"をdstに追加し、拡張されたバッファを返します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	b := []byte("bool:")
	b = strconv.AppendBool(b, true)
	fmt.Println(string(b))

}
Output:

bool:true

func AppendFloat

func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte

AppendFloatは、FormatFloatによって生成された浮動小数点数fの文字列形式をdstに追加し、拡張されたバッファを返します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	b32 := []byte("float32:")
	b32 = strconv.AppendFloat(b32, 3.1415926535, 'E', -1, 32)
	fmt.Println(string(b32))

	b64 := []byte("float64:")
	b64 = strconv.AppendFloat(b64, 3.1415926535, 'E', -1, 64)
	fmt.Println(string(b64))

}
Output:

float32:3.1415927E+00
float64:3.1415926535E+00

func AppendInt

func AppendInt(dst []byte, i int64, base int) []byte

AppendIntは、FormatIntによって生成された整数iの文字列形式をdstに追加し、拡張されたバッファを返します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	b10 := []byte("int (base 10):")
	b10 = strconv.AppendInt(b10, -42, 10)
	fmt.Println(string(b10))

	b16 := []byte("int (base 16):")
	b16 = strconv.AppendInt(b16, -42, 16)
	fmt.Println(string(b16))

}
Output:

int (base 10):-42
int (base 16):-2a

func AppendQuote

func AppendQuote(dst []byte, s string) []byte

AppendQuoteは、sを表すダブルクォートで囲まれたGo文字列リテラル(Quoteによって生成されたもの)をdstに追加し、拡張されたバッファを返します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	b := []byte("quote:")
	b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`)
	fmt.Println(string(b))

}
Output:

quote:"\"Fran & Freddie's Diner\""

func AppendQuoteRune

func AppendQuoteRune(dst []byte, r rune) []byte

AppendQuoteRuneは、runeを表すシングルクォートで囲まれたGo文字リテラル(QuoteRuneによって生成されたもの)をdstに追加し、拡張されたバッファを返します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	b := []byte("rune:")
	b = strconv.AppendQuoteRune(b, '☺')
	fmt.Println(string(b))

}
Output:

rune:'☺'

func AppendQuoteRuneToASCII

func AppendQuoteRuneToASCII(dst []byte, r rune) []byte

AppendQuoteRuneToASCIIは、runeを表すシングルクォートで囲まれたGo文字リテラル(QuoteRuneToASCIIによって生成されたもの)をdstに追加し、拡張されたバッファを返します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	b := []byte("rune (ascii):")
	b = strconv.AppendQuoteRuneToASCII(b, '☺')
	fmt.Println(string(b))

}
Output:

rune (ascii):'\u263a'

func AppendQuoteRuneToGraphic added in v1.6.0

func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte

AppendQuoteRuneToGraphicは、runeを表すシングルクォートで囲まれたGo文字リテラル(QuoteRuneToGraphicによって生成されたもの)をdstに追加し、拡張されたバッファを返します。

func AppendQuoteToASCII

func AppendQuoteToASCII(dst []byte, s string) []byte

AppendQuoteToASCIIは、sを表すダブルクォートで囲まれたGo文字列リテラル(QuoteToASCIIによって生成されたもの)をdstに追加し、拡張されたバッファを返します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	b := []byte("quote (ascii):")
	b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`)
	fmt.Println(string(b))

}
Output:

quote (ascii):"\"Fran & Freddie's Diner\""

func AppendQuoteToGraphic added in v1.6.0

func AppendQuoteToGraphic(dst []byte, s string) []byte

AppendQuoteToGraphicは、sを表すダブルクォートで囲まれたGo文字列リテラル(QuoteToGraphicによって生成されたもの)をdstに追加し、拡張されたバッファを返します。

func AppendUint

func AppendUint(dst []byte, i uint64, base int) []byte

AppendUintは、FormatUintによって生成された符号なし整数iの文字列形式をdstに追加し、拡張されたバッファを返します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	b10 := []byte("uint (base 10):")
	b10 = strconv.AppendUint(b10, 42, 10)
	fmt.Println(string(b10))

	b16 := []byte("uint (base 16):")
	b16 = strconv.AppendUint(b16, 42, 16)
	fmt.Println(string(b16))

}
Output:

uint (base 10):42
uint (base 16):2a

func Atoi

func Atoi(s string) (int, error)

AtoiはParseInt(s, 10, 0)と同じであり、int型に変換されます。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	v := "10"
	if s, err := strconv.Atoi(v); err == nil {
		fmt.Printf("%T, %v", s, s)
	}

}
Output:

int, 10

func CanBackquote

func CanBackquote(s string) bool

CanBackquoteは、タブ以外の制御文字を含まない単一行のバッククォート文字列として、文字列sを変更せずに表現できるかどうかを報告します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ☺"))
	fmt.Println(strconv.CanBackquote("`can't backquote this`"))

}
Output:

true
false

func FormatBool

func FormatBool(b bool) string

FormatBoolはbの値に応じて"true"または"false"を返します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	v := true
	s := strconv.FormatBool(v)
	fmt.Printf("%T, %v\n", s, s)

}
Output:

string, true

func FormatComplex added in v1.15.0

func FormatComplex(c complex128, fmt byte, prec, bitSize int) string

FormatComplexは複素数cを,(a+bi)という形式の文字列に変換します.ここでaとbは実部と虚部を表し,フォーマットfmtと精度precに従って整形されます.

フォーマットfmtと精度precは,FormatFloatの意味と同じです.元の値がcomplex64の場合はbitSizeが64で,complex128の場合は128であることを前提にして結果を丸めます.

func FormatFloat

func FormatFloat(f float64, fmt byte, prec, bitSize int) string

FormatFloatは、浮動小数点数fを、フォーマットfmtと精度precに従って文字列に変換します。 それは、元の値がbitSizeビット(float32の場合は32、float64の場合は64)の浮動小数点値から得られたものと仮定して、 結果を四捨五入します。

フォーマットfmtは、次のいずれかです。 'b' (-ddddp±ddd、2進指数表記), 'e' (-d.dddde±dd、10進指数表記), 'E' (-d.ddddE±dd、10進指数表記), 'f' (-ddd.dddd、指数なし), 'g' (指数が大きい場合は'e'、そうでない場合は'f'), 'G' (指数が大きい場合は'E'、そうでない場合は'f'), 'x' (-0xd.ddddp±ddd、16進数の小数部と2進指数表記), または 'X' (-0Xd.ddddP±ddd、16進数の小数部と2進指数表記)。

精度precは、'e'、'E'、'f'、'g'、'G'、'x'、および'X'フォーマットによって出力される指数を除く桁数を制御します。 'e'、'E'、'f'、'x'、および'X'の場合、小数点以下の桁数です。 'g'および'G'の場合、最大有効桁数です(末尾のゼロは削除されます)。 特別な精度-1は、ParseFloatがfを正確に返すために必要な最小桁数を使用します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	v := 3.1415926535

	s32 := strconv.FormatFloat(v, 'E', -1, 32)
	fmt.Printf("%T, %v\n", s32, s32)

	s64 := strconv.FormatFloat(v, 'E', -1, 64)
	fmt.Printf("%T, %v\n", s64, s64)

	// fmt.Printlnはこれらの引数を使用して浮動小数点数を出力します。
	fmt64 := strconv.FormatFloat(v, 'g', -1, 64)
	fmt.Printf("%T, %v\n", fmt64, fmt64)

}
Output:

string, 3.1415927E+00
string, 3.1415926535E+00
string, 3.1415926535

func FormatInt

func FormatInt(i int64, base int) string

FormatIntは、2 <= base <= 36の場合、iの文字列表現を指定された基数で返します。 結果は、10以上の桁の値に対して小文字の 'a' から 'z' を使用します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	v := int64(-42)

	s10 := strconv.FormatInt(v, 10)
	fmt.Printf("%T, %v\n", s10, s10)

	s16 := strconv.FormatInt(v, 16)
	fmt.Printf("%T, %v\n", s16, s16)

}
Output:

string, -42
string, -2a

func FormatUint

func FormatUint(i uint64, base int) string

FormatUintは、2 <= base <= 36の場合、iの文字列表現を指定された基数で返します。 結果は、10以上の桁の値に対して小文字の 'a' から 'z' を使用します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	v := uint64(42)

	s10 := strconv.FormatUint(v, 10)
	fmt.Printf("%T, %v\n", s10, s10)

	s16 := strconv.FormatUint(v, 16)
	fmt.Printf("%T, %v\n", s16, s16)

}
Output:

string, 42
string, 2a

func IsGraphic added in v1.6.0

func IsGraphic(r rune) bool

IsGraphicは、Unicodeによってグラフィックとして定義されたルーンかどうかを報告します。 このような文字には、カテゴリL、M、N、P、S、およびZsからの文字、数字、句読点、記号、スペースが含まれます。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	shamrock := strconv.IsGraphic('☘')
	fmt.Println(shamrock)

	a := strconv.IsGraphic('a')
	fmt.Println(a)

	bel := strconv.IsGraphic('\007')
	fmt.Println(bel)

}
Output:

true
true
false

func IsPrint

func IsPrint(r rune) bool

IsPrintは、文字がGoによって印刷可能と定義されているかどうかを報告します。 これは、unicode.IsPrintと同じ定義で、文字、数字、句読点、記号、ASCIIスペースを含みます。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	c := strconv.IsPrint('\u263a')
	fmt.Println(c)

	bel := strconv.IsPrint('\007')
	fmt.Println(bel)

}
Output:

true
false

func Itoa

func Itoa(i int) string

Itoaは、FormatInt(int64(i), 10)と同等です。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	i := 10
	s := strconv.Itoa(i)
	fmt.Printf("%T, %v\n", s, s)

}
Output:

string, 10

func ParseBool

func ParseBool(str string) (bool, error)

ParseBoolは文字列で表されるブール値を返します。 1、t、T、TRUE、true、True、0、f、F、FALSE、false、Falseを受け入れます。 その他の値はエラーを返します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	v := "true"
	if s, err := strconv.ParseBool(v); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

}
Output:

bool, true

func ParseComplex added in v1.15.0

func ParseComplex(s string, bitSize int) (complex128, error)

ParseComplexは文字列sを、bitSizeで指定された精度で複素数に変換します。 bitSizeが64の場合、結果は型complex128になりますが、値を変更せずにcomplex64に変換することができます。

sに表される数値は、N、Ni、またはN±Niの形式でなければなりません。ここで、NはParseFloatで認識される浮動小数点数を表し、iは虚数成分です。 2つ目のNが非負である場合、±によって示される2つの成分の間に+記号が必要です。2つ目のNがNaNである場合、+記号のみが受け入れられます。 形式は括弧で囲んでも良く、スペースを含んではいけません。 変換される複素数は、ParseFloatによって変換された2つの成分から構成されます。

ParseComplexが返すエラーは、*NumErrorという具体的な型であり、err.Num = sとなります。

sが構文的に正しくない場合、ParseComplexはerr.Err = ErrSyntaxを返します。

sが構文的に正しくなっているが、いずれかの成分が与えられた成分のサイズで最大の浮動小数点数から1/2 ULP以上離れている場合、 ParseComplexはerr.Err = ErrRangeとc = ±Infを返します。

func ParseFloat

func ParseFloat(s string, bitSize int) (float64, error)

ParseFloatは、文字列sを、bitSizeで指定された精度の浮動小数点数に変換します。 bitSize=32の場合、結果はfloat64型のままですが、値を変更せずにfloat32型に変換できます。

ParseFloatは、浮動小数点リテラル のGo構文で定義された10進数および16進数の浮動小数点数を受け入れます。 sが正しく形成され、有効な浮動小数点数に近い場合、ParseFloatはIEEE754のバイアスのない丸めを使用して最も近い浮動小数点数を返します。 (16進数の浮動小数点値を解析する場合、16進数表現にビットが多すぎてマンティッサに収まらない場合にのみ丸めが行われます。)

ParseFloatが返すエラーは、*NumErrorの具体的な型であり、err.Num = sを含みます。

sが構文的に正しくない場合、ParseFloatはerr.Err = ErrSyntaxを返します。

sが構文的に正しく、与えられたサイズの最大浮動小数点数から1/2 ULP以上離れている場合、 ParseFloatはf = ±Inf、err.Err = ErrRangeを返します。

ParseFloatは、文字列 "NaN" および (可能な場合は符号付きの) 文字列 "Inf" および "Infinity" を、 それぞれ特別な浮動小数点値として認識します。大文字小文字は区別されません。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	v := "3.1415926535"
	if s, err := strconv.ParseFloat(v, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat(v, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("NaN", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	// ParseFloat は大文字小文字を区別しない
	if s, err := strconv.ParseFloat("nan", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("inf", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("+Inf", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("-Inf", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("-0", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("+0", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

}
Output:

float64, 3.1415927410125732
float64, 3.1415926535
float64, NaN
float64, NaN
float64, +Inf
float64, +Inf
float64, -Inf
float64, -0
float64, 0

func ParseInt

func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseIntは与えられた基数(0、2から36)とビットサイズ(0から64)で文字列sを解釈し、対応する値iを返します。

文字列は先頭に符号 "+" または "-" を持つことができます。

基数引数が0の場合、真の基数は符号の後に続く文字列の接頭辞で推測されます(存在する場合):"0b"の場合は2、"0"または"0o"の場合は8、"0x"の場合は16、それ以外の場合は10です。また、基数0の場合だけアンダースコア文字が許可されます。これはGoの構文で定義されている整数リテラルです。

bitSize引数は結果が適合する必要のある整数型を指定します。ビットサイズ0、8、16、32、64はint、int8、int16、int32、int64に対応します。bitSizeが0未満または64を超える場合、エラーが返されます。

ParseIntが返すエラーは具体的な型*NumErrorを持ち、err.Num = sとなります。sが空であるか無効な数字を含んでいる場合、err.Err = ErrSyntaxとなり返される値は0です。sに対応する値を指定のサイズの符号付き整数で表現することができない場合、err.Err = ErrRangeとなり、返される値は適切なbitSizeと符号の最大の大きさの整数です。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	v32 := "-354634382"
	if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

	v64 := "-3546343826724305832"
	if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

}
Output:

int64, -354634382
int64, -3546343826724305832

func ParseUint

func ParseUint(s string, base int, bitSize int) (uint64, error)

ParseUintはParseIntと同じですが、符号の接頭辞は許可されていません。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	v := "42"
	if s, err := strconv.ParseUint(v, 10, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseUint(v, 10, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

}
Output:

uint64, 42
uint64, 42

func Quote

func Quote(s string) string

Quoteは、sを表すダブルクォートで囲まれたGo文字列リテラルを返します。 返される文字列は、制御文字およびIsPrintによって定義された印刷不可能な文字のために、 Goエスケープシーケンス(\t、\n、\xFF、\u0100)を使用します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	// この文字列リテラルにはタブ文字が含まれています。
	s := strconv.Quote(`"Fran & Freddie's Diner	☺"`)
	fmt.Println(s)

}
Output:

"\"Fran & Freddie's Diner\t☺\""

func QuoteRune

func QuoteRune(r rune) string

QuoteRuneは、runeを表すシングルクォートで囲まれたGo文字リテラルを返します。 返される文字列は、制御文字およびIsPrintによって定義された印刷不可能な文字のために、 Goエスケープシーケンス(\t、\n、\xFF、\u0100)を使用します。 rが有効なUnicodeコードポイントでない場合、Unicode置換文字U+FFFDとして解釈されます。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	s := strconv.QuoteRune('☺')
	fmt.Println(s)

}
Output:

'☺'

func QuoteRuneToASCII

func QuoteRuneToASCII(r rune) string

QuoteRuneToASCIIは、runeを表すシングルクォートで囲まれたGo文字リテラルを返します。 返される文字列は、制御文字およびIsPrintによって定義された印刷不可能な文字のために、 Goエスケープシーケンス(\t、\n、\xFF、\u0100)を使用します。 rが有効なUnicodeコードポイントでない場合、Unicode置換文字U+FFFDとして解釈されます。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	s := strconv.QuoteRuneToASCII('☺')
	fmt.Println(s)

}
Output:

'\u263a'

func QuoteRuneToGraphic added in v1.6.0

func QuoteRuneToGraphic(r rune) string

QuoteRuneToGraphicは、runeを表すシングルクォートで囲まれたGo文字リテラルを返します。 返される文字列は、Unicodeのグラフィック文字(IsGraphicによって定義される)を変更せず、 非グラフィック文字に対してGoエスケープシーケンス(\t、\n、\xFF、\u0100)を使用します。 rが有効なUnicodeコードポイントでない場合、Unicode置換文字U+FFFDとして解釈されます。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	s := strconv.QuoteRuneToGraphic('☺')
	fmt.Println(s)

	s = strconv.QuoteRuneToGraphic('\u263a')
	fmt.Println(s)

	s = strconv.QuoteRuneToGraphic('\u000a')
	fmt.Println(s)

	s = strconv.QuoteRuneToGraphic('	') // タブ文字
	fmt.Println(s)

}
Output:

'☺'
'☺'
'\n'
'\t'

func QuoteToASCII

func QuoteToASCII(s string) string

QuoteToASCIIは、sを表すダブルクォートで囲まれたGo文字列リテラルを返します。 返される文字列は、制御文字およびIsPrintによって定義された印刷不可能な文字のために、 Goエスケープシーケンス(\t、\n、\xFF、\u0100)を使用します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	// この文字列リテラルにはタブ文字が含まれています。
	s := strconv.QuoteToASCII(`"Fran & Freddie's Diner	☺"`)
	fmt.Println(s)

}
Output:

"\"Fran & Freddie's Diner\t\u263a\""

func QuoteToGraphic added in v1.6.0

func QuoteToGraphic(s string) string

QuoteToGraphicは、sを表すダブルクォートで囲まれたGo文字列リテラルを返します。 返される文字列は、Unicodeのグラフィック文字(IsGraphicによって定義される)を変更せず、 非グラフィック文字に対してGoエスケープシーケンス(\t、\n、\xFF、\u0100)を使用します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	s := strconv.QuoteToGraphic("☺")
	fmt.Println(s)

	// この文字列リテラルにはタブ文字が含まれています。
	s = strconv.QuoteToGraphic("This is a \u263a	\u000a")
	fmt.Println(s)

	s = strconv.QuoteToGraphic(`" This is a ☺ \n "`)
	fmt.Println(s)

}
Output:

"☺"
"This is a ☺\t\n"
"\" This is a ☺ \\n \""

func QuotedPrefix added in v1.17.0

func QuotedPrefix(s string) (string, error)

QuotedPrefixは、sのプレフィックスにある引用符で囲まれた文字列(Unquoteで理解される形式)を返します。 sが有効な引用符で囲まれた文字列で始まらない場合、QuotedPrefixはエラーを返します。

func Unquote

func Unquote(s string) (string, error)

Unquoteは、sを単一引用符、二重引用符、またはバッククォートで囲まれたGo文字列リテラルとして解釈し、sが引用する文字列値を返します。 (sが単一引用符で囲まれている場合、それはGo文字リテラルであることに注意してください。Unquoteは対応する1文字の文字列を返します。)

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	s, err := strconv.Unquote("You can't unquote a string without quotes")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("\"The string must be either double-quoted\"")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("`or backquoted.`")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("'\u263a'") // 単一の文字はシングルクォーテーション内でのみ許可されています。
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("'\u2639\u2639'")
	fmt.Printf("%q, %v\n", s, err)

}
Output:

"", invalid syntax
"The string must be either double-quoted", <nil>
"or backquoted.", <nil>
"☺", <nil>
"", invalid syntax

func UnquoteChar

func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)

UnquoteCharは、文字列sによって表されるエスケープされた文字列または文字リテラルの最初の文字またはバイトをデコードします。 4つの値を返します。

1. value:デコードされたUnicodeコードポイントまたはバイト値 2. multibyte:デコードされた文字がマルチバイトUTF-8表現を必要とするかどうかを示すブール値 3. tail:文字の後の残りの文字列 4. エラー:文字が構文的に有効である場合はnilになります。

2番目の引数であるquoteは、解析されるリテラルの種類を指定し、許可されるエスケープされた引用符文字を決定します。 シングルクォートに設定すると、\'のシーケンスを許可し、エスケープされていない'を許可しません。 ダブルクォートに設定すると、\"を許可し、エスケープされていない"を許可しません。 ゼロに設定すると、どちらのエスケープも許可せず、両方の引用符文字がエスケープされていない状態で現れることを許可します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/log"
	"github.com/shogo82148/std/strconv"
)

func main() {
	v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"')
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("value:", string(v))
	fmt.Println("multibyte:", mb)
	fmt.Println("tail:", t)

}
Output:

value: "
multibyte: false
tail: Fran & Freddie's Diner\"

Types

type NumError

type NumError struct {
	Func string
	Num  string
	Err  error
}

NumErrorは変換に失敗したことを記録します。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/strconv"
)

func main() {
	str := "Not a number"
	if _, err := strconv.ParseFloat(str, 64); err != nil {
		e := err.(*strconv.NumError)
		fmt.Println("Func:", e.Func)
		fmt.Println("Num:", e.Num)
		fmt.Println("Err:", e.Err)
		fmt.Println(err)
	}

}
Output:

Func: ParseFloat
Num: Not a number
Err: invalid syntax
strconv.ParseFloat: parsing "Not a number": invalid syntax

func (*NumError) Error

func (e *NumError) Error() string

func (*NumError) Unwrap added in v1.14.0

func (e *NumError) Unwrap() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL