bytes

package
v1.21.3 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

パッケージbytesはバイトスライスの操作のための関数を実装します。 これは[strings]パッケージの機能に類似しています。

Index

Examples

Constants

View Source
const MinRead = 512

MinReadはBuffer.ReadFromによってRead呼び出しに渡される最小のスライスサイズです。 Bufferは、rの内容を保持するために必要なものを超えて少なくともMinReadバイトを持っている限り、ReadFromは基礎となるバッファを拡大しません。

Variables

View Source
var ErrTooLarge = errors.New("bytes.Buffer: too large")

ErrTooLargeは、バッファにデータを格納するためのメモリを割り当てることができない場合にpanicに渡されます。

Functions

func Clone added in v1.20.0

func Clone(b []byte) []byte

Cloneはb[:len(b)]のコピーを返します。 結果には余分な未使用の容量があるかもしれません。 Clone(nil)はnilを返します。

Example
package main

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

func main() {
	b := []byte("abc")
	clone := bytes.Clone(b)
	fmt.Printf("%s\n", clone)
	clone[0] = 'd'
	fmt.Printf("%s\n", b)
	fmt.Printf("%s\n", clone)
}
Output:

abc
abc
dbc

func Compare

func Compare(a, b []byte) int

Compare関数は2つのバイトスライスを辞書的に比較して整数を返します。 a == bの場合は0、a < bの場合は-1、a > bの場合は+1となります。 nilの引数は空スライスと同等です。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
)

func main() {
	// Interpret Compare's result by comparing it to zero.
	var a, b []byte
	if bytes.Compare(a, b) < 0 {
		// a less b
	}
	if bytes.Compare(a, b) <= 0 {
		// a less or equal b
	}
	if bytes.Compare(a, b) > 0 {
		// a greater b
	}
	if bytes.Compare(a, b) >= 0 {
		// a greater or equal b
	}

	// Prefer Equal to Compare for equality comparisons.
	if bytes.Equal(a, b) {
		// a equal b
	}
	if !bytes.Equal(a, b) {
		// a not equal b
	}
}
Output:

func Contains

func Contains(b, subslice []byte) bool

subsliceがb内に含まれているかどうかを報告します。

Example
package main

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

func main() {
	fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo")))
	fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar")))
	fmt.Println(bytes.Contains([]byte("seafood"), []byte("")))
	fmt.Println(bytes.Contains([]byte(""), []byte("")))
}
Output:

true
false
true
true

func ContainsAny added in v1.7.0

func ContainsAny(b []byte, chars string) bool

ContainsAnyは、chars内のUTF-8エンコードされたコードポイントのいずれかがb内に含まれているかどうかを報告します。

Example
package main

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

func main() {
	fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "fÄo!"))
	fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "去是伟大的."))
	fmt.Println(bytes.ContainsAny([]byte("I like seafood."), ""))
	fmt.Println(bytes.ContainsAny([]byte(""), ""))
}
Output:

true
true
false
false

func ContainsFunc added in v1.21.0

func ContainsFunc(b []byte, f func(rune) bool) bool

ContainsFuncは、UTF-8エンコードされたコードポイントの中で、bのどれかがf(r)を満たすかどうかを報告します。

func ContainsRune added in v1.7.0

func ContainsRune(b []byte, r rune) bool

ContainsRuneは、UTF-8でエンコードされたバイトスライスbにルーンが含まれているかどうかを報告します。

Example
package main

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

func main() {
	fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f'))
	fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö'))
	fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '大'))
	fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '!'))
	fmt.Println(bytes.ContainsRune([]byte(""), '@'))
}
Output:

true
false
true
true
false

func Count

func Count(s, sep []byte) int

Count は s において非重複の sep の出現回数を数えます。 もし sep が空のスライスなら、Count は s 中の UTF-8 エンコードされたコードポイントの数に 1 を加えた値を返します。

Example
package main

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

func main() {
	fmt.Println(bytes.Count([]byte("cheese"), []byte("e")))
	fmt.Println(bytes.Count([]byte("five"), []byte(""))) // before & after each rune
}
Output:

3
5

func Cut added in v1.18.0

func Cut(s, sep []byte) (before, after []byte, found bool)

最初の「sep」のインスタンス周りのスライス「s」を切り取り、 sepの前と後のテキストを返します。 見つかった結果は「sep」が「s」に現れるかどうかを報告します。 もし「sep」が「s」に現れない場合、cutは「s」とnil、falseを返します。

Cutは元のスライス「s」のスライスを返します、コピーではありません。

Example
package main

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

func main() {
	show := func(s, sep string) {
		before, after, found := bytes.Cut([]byte(s), []byte(sep))
		fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found)
	}
	show("Gopher", "Go")
	show("Gopher", "ph")
	show("Gopher", "er")
	show("Gopher", "Badger")
}
Output:

Cut("Gopher", "Go") = "", "pher", true
Cut("Gopher", "ph") = "Go", "er", true
Cut("Gopher", "er") = "Goph", "", true
Cut("Gopher", "Badger") = "Gopher", "", false

func CutPrefix added in v1.20.0

func CutPrefix(s, prefix []byte) (after []byte, found bool)

CutPrefixは与えられた先頭接頭辞のバイトスライスを取り除き、 接頭辞が見つかったかどうかを報告します。 もしsが接頭辞で始まっていない場合、CutPrefixはs、falseを返します。 もし接頭辞が空のバイトスライスの場合、CutPrefixはs、trueを返します。

CutPrefixは元のスライスsの断片を返します、コピーではありません。

Example
package main

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

func main() {
	show := func(s, sep string) {
		after, found := bytes.CutPrefix([]byte(s), []byte(sep))
		fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, sep, after, found)
	}
	show("Gopher", "Go")
	show("Gopher", "ph")
}
Output:

CutPrefix("Gopher", "Go") = "pher", true
CutPrefix("Gopher", "ph") = "Gopher", false

func CutSuffix added in v1.20.0

func CutSuffix(s, suffix []byte) (before []byte, found bool)

CutSuffixは与えられた終了サフィックスのバイトスライスを除いたsを返し、そのサフィックスが見つかったかどうかを報告します。 もしsがサフィックスで終わらない場合、CutSuffixはs、falseを返します。 もしサフィックスが空のバイトスライスである場合、CutSuffixはs、trueを返します。

CutSuffixは元のスライスsのスライスを返しますが、コピーではありません。

Example
package main

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

func main() {
	show := func(s, sep string) {
		before, found := bytes.CutSuffix([]byte(s), []byte(sep))
		fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, sep, before, found)
	}
	show("Gopher", "Go")
	show("Gopher", "er")
}
Output:

CutSuffix("Gopher", "Go") = "Gopher", false
CutSuffix("Gopher", "er") = "Goph", true

func Equal

func Equal(a, b []byte) bool

Equalは、aとbが同じ長さで同じバイトを含むかどうかを報告します。 nilの引数は空のスライスと等価です。

Example
package main

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

func main() {
	fmt.Println(bytes.Equal([]byte("Go"), []byte("Go")))
	fmt.Println(bytes.Equal([]byte("Go"), []byte("C++")))
}
Output:

true
false

func EqualFold

func EqualFold(s, t []byte) bool

EqualFoldは、UTF-8文字列として解釈されたsとtが、単純なUnicodeの大文字小文字を区別しない比較で等しいかどうかを報告します。これは、大文字小文字を区別しない形式の一般的なUnicodeです。

Example
package main

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

func main() {
	fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
}
Output:

true

func Fields

func Fields(s []byte) [][]byte

FieldsはUTF-8でエンコードされたコードポイントのシーケンスとしてsを解釈します。 それはunicode.IsSpaceによって定義される1つ以上の連続したホワイトスペース文字の各インスタンスを区切り、sのサブスライスのスライスまたは空のスライスを返します。sには空白だけが含まれる場合。

Example
package main

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

func main() {
	fmt.Printf("Fields are: %q", bytes.Fields([]byte("  foo bar  baz   ")))
}
Output:

Fields are: ["foo" "bar" "baz"]

func FieldsFunc

func FieldsFunc(s []byte, f func(rune) bool) [][]byte

FieldsFuncは、sをUTF-8でエンコードされたコードポイントのシーケンスとして解釈します。 それは、f(c)を満たすコードポイントcの連続を各ランでsを分割し、sのサブスライスのスライスを返します。 sのすべてのコードポイントがf(c)を満たすか、またはlen(s) == 0の場合、空のスライスが返されます。

FieldsFuncは、f(c)をどの順序で呼び出すかについて保証はなく、fは常に同じ値を返すと仮定しています。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/unicode"
)

func main() {
	f := func(c rune) bool {
		return !unicode.IsLetter(c) && !unicode.IsNumber(c)
	}
	fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte("  foo1;bar2,baz3..."), f))
}
Output:

Fields are: ["foo1" "bar2" "baz3"]

func HasPrefix

func HasPrefix(s, prefix []byte) bool

HasPrefixは、バイトスライスsがprefixで始まるかどうかをテストします。

Example
package main

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

func main() {
	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go")))
	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C")))
	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("")))
}
Output:

true
false
true

func HasSuffix

func HasSuffix(s, suffix []byte) bool

HasSuffixは、バイトスライスsが接尾辞で終わっているかをテストします。

Example
package main

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

func main() {
	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go")))
	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O")))
	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami")))
	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("")))
}
Output:

true
false
false
true

func Index

func Index(s, sep []byte) int

Indexは、sの最初のsepのインスタンスのインデックスを返します。sepがsに存在しない場合は、-1を返します。

Example
package main

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

func main() {
	fmt.Println(bytes.Index([]byte("chicken"), []byte("ken")))
	fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr")))
}
Output:

4
-1

func IndexAny

func IndexAny(s []byte, chars string) int

IndexAnyはsをUTF-8エンコードされたUnicodeのコードポイントのシーケンスとして解釈します。 sの中でcharsのいずれかのUnicodeコードポイントの最初の出現のバイトインデックスを返します。 charsが空であるか、共通のコードポイントがない場合は-1を返します。

Example
package main

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

func main() {
	fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy"))
	fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy"))
}
Output:

2
-1

func IndexByte

func IndexByte(b []byte, c byte) int

IndexByteはb内の最初のcのインスタンスのインデックスを返します。もしcがbに存在しない場合は、-1を返します。

Example
package main

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

func main() {
	fmt.Println(bytes.IndexByte([]byte("chicken"), byte('k')))
	fmt.Println(bytes.IndexByte([]byte("chicken"), byte('g')))
}
Output:

4
-1

func IndexFunc

func IndexFunc(s []byte, f func(r rune) bool) int

IndexFuncは、sをUTF-8エンコードされたコードポイントのシーケンスとして解釈します。 sの中でf(c)を満たす最初のUnicodeコードポイントのバイトインデックスを返します。 該当するコードポイントがない場合は-1を返します。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/unicode"
)

func main() {
	f := func(c rune) bool {
		return unicode.Is(unicode.Han, c)
	}
	fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f))
	fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f))
}
Output:

7
-1

func IndexRune

func IndexRune(s []byte, r rune) int

IndexRuneはsをUTF-8でエンコードされたコードポイントのシーケンスとして解釈します。 sの中で指定されたルーンの最初の出現のバイトインデックスを返します。 sにルーンが含まれていない場合は-1を返します。 rがutf8.RuneErrorである場合、無効なUTF-8バイトシーケンスの最初のインスタンスを返します。

Example
package main

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

func main() {
	fmt.Println(bytes.IndexRune([]byte("chicken"), 'k'))
	fmt.Println(bytes.IndexRune([]byte("chicken"), 'd'))
}
Output:

4
-1

func Join

func Join(s [][]byte, sep []byte) []byte

Join関数は、sの要素を連結して新しいバイトスライスを作成します。結果のスライスの要素間にはセパレーターsepが配置されます。

Example
package main

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

func main() {
	s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
	fmt.Printf("%s", bytes.Join(s, []byte(", ")))
}
Output:

foo, bar, baz

func LastIndex

func LastIndex(s, sep []byte) int

LastIndex関数は、s内のsepの最後のインスタンスのインデックスを返します。sepがs内に存在しない場合は、-1を返します。

Example
package main

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

func main() {
	fmt.Println(bytes.Index([]byte("go gopher"), []byte("go")))
	fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go")))
	fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent")))
}
Output:

0
3
-1

func LastIndexAny

func LastIndexAny(s []byte, chars string) int

LastIndexAnyは、sをUTF-8でエンコードされたUnicodeコードポイントの シーケンスとして解釈します。charsに含まれる任意のUnicodeコードポイントの 最後の出現のバイトインデックスを返します。charsが空である場合や、 共通のコードポイントが存在しない場合は、-1を返します。

Example
package main

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

func main() {
	fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp"))
	fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大"))
	fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!."))
}
Output:

5
3
-1

func LastIndexByte added in v1.5.0

func LastIndexByte(s []byte, c byte) int

LastIndexByteは、cがs内で最後に出現するインデックスを返します。cがs内に存在しない場合は-1を返します。

Example
package main

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

func main() {
	fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('g')))
	fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('r')))
	fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('z')))
}
Output:

3
8
-1

func LastIndexFunc

func LastIndexFunc(s []byte, f func(r rune) bool) int

LastIndexFuncはsをUTF-8でエンコードされたコードポイントのシーケンスとして解釈します。 f(c)を満たす最後のUnicodeコードポイントのバイトインデックスを、f(c)を満たすものがない場合は-1を返します。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/unicode"
)

func main() {
	fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsLetter))
	fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsPunct))
	fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsNumber))
}
Output:

8
9
-1

func Map

func Map(mapping func(r rune) rune, s []byte) []byte

Map関数は、与えられたマッピング関数に基づいて、バイトスライスsのすべての文字が変更されたコピーを返します。 マッピング関数が負の値を返すと、文字は置換せずにバイトスライスから削除されます。 sと出力の文字はUTF-8エンコードされたコードポイントとして解釈されます。

Example
package main

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

func main() {
	rot13 := func(r rune) rune {
		switch {
		case r >= 'A' && r <= 'Z':
			return 'A' + (r-'A'+13)%26
		case r >= 'a' && r <= 'z':
			return 'a' + (r-'a'+13)%26
		}
		return r
	}
	fmt.Printf("%s\n", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher...")))
}
Output:

'Gjnf oevyyvt naq gur fyvgul tbcure...

func Repeat

func Repeat(b []byte, count int) []byte

Repeat は、bのcount回のコピーからなる新しいバイトスライスを返します。

countが負数であるか、(len(b) * count)の結果がオーバーフローする場合、パニックが発生します。

Example
package main

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

func main() {
	fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2))
}
Output:

banana

func Replace

func Replace(s, old, new []byte, n int) []byte

Replaceは、スライスsの最初のn個の重ならない oldのインスタンスをnewで置き換えたスライスのコピーを返します。 oldが空の場合、スライスの先頭とUTF-8シーケンスの後に一致し、 kランスライスに対してk+1回の置換がされます。 nが負の場合、置換の数に制限はありません。

Example
package main

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

func main() {
	fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2))
	fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1))
}
Output:

oinky oinky oink
moo moo moo

func ReplaceAll added in v1.12.0

func ReplaceAll(s, old, new []byte) []byte

ReplaceAllは、スライスsのすべての非重複インスタンスをoldからnewに置き換えたスライスのコピーを返します。 oldが空の場合、スライスの先頭とUTF-8シーケンスの後に一致し、kランスライスに対してk+1回の置換が行われます。

Example
package main

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

func main() {
	fmt.Printf("%s\n", bytes.ReplaceAll([]byte("oink oink oink"), []byte("oink"), []byte("moo")))
}
Output:

moo moo moo

func Runes

func Runes(s []byte) []rune

RunesはUTF-8でエンコードされたコードポイントのシーケンスとしてsを解釈します。 sと同等のルーン(Unicodeのコードポイント)のスライスを返します。

Example
package main

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

func main() {
	rs := bytes.Runes([]byte("go gopher"))
	for _, r := range rs {
		fmt.Printf("%#U\n", r)
	}
}
Output:

U+0067 'g'
U+006F 'o'
U+0020 ' '
U+0067 'g'
U+006F 'o'
U+0070 'p'
U+0068 'h'
U+0065 'e'
U+0072 'r'

func Split

func Split(s, sep []byte) [][]byte

Split関数は、sをsepで区切ったすべてのサブスライスから成るスライスを返します。 sepが空の場合、Split関数はUTF-8シーケンスごとに区切ります。 これは、SplitN関数にカウント-1を指定した場合と同等です。

最初の区切り文字で区切る場合は、Cut関数を参照してください。

Example
package main

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

func main() {
	fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(",")))
	fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a ")))
	fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte("")))
	fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins")))
}
Output:

["a" "b" "c"]
["" "man " "plan " "canal panama"]
[" " "x" "y" "z" " "]
[""]

func SplitAfter

func SplitAfter(s, sep []byte) [][]byte

SplitAfterは、sをsepの各インスタンスの後にスライスし、それらのサブスライスのスライスを返します。 sepが空の場合、UTF-8のシーケンスの後に分割します。 これは、countが-1のSplitAfterNと同等です。

Example
package main

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

func main() {
	fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(",")))
}
Output:

["a," "b," "c"]

func SplitAfterN

func SplitAfterN(s, sep []byte, n int) [][]byte

SplitAfterNはsをsepの各インスタンスの後ろでサブスライスに分割し、それらのサブスライスのスライスを返します。 sepが空である場合、SplitAfterNはUTF-8シーケンスの後ろで分割します。 countは返すサブスライスの数を決定します:

n > 0:最大nのサブスライス;最後のサブスライスは分割されていない残りになります。 n == 0:結果はnilです(サブスライスはゼロ個) n < 0:すべてのサブスライス

Example
package main

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

func main() {
	fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2))
}
Output:

["a," "b,c"]

func SplitN

func SplitN(s, sep []byte, n int) [][]byte

SplitNは、sをsepで区切り、そのセパレーターの間のサブスライスのスライスを返します。 sepが空の場合、SplitNは各UTF-8シーケンスの後に分割します。 countは返すサブスライスの数を決定します: n> 0:最大でn個のサブスライス;最後のサブスライスは分割パートが含まれます。 n == 0:結果はnilです(ゼロのサブスライス) n < 0:すべてのサブスライス 最初のセパレーターの周りで分割するには、Cutを参照してください。

Example
package main

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

func main() {
	fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2))
	z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0)
	fmt.Printf("%q (nil = %v)\n", z, z == nil)
}
Output:

["a" "b,c"]
[] (nil = true)

func Title

func Title(s []byte) []byte

TitleはUTF-8でエンコードされたバイト列sをUnicodeの文字として扱い、単語の先頭にあるすべての文字をタイトルケースにマッピングしたコピーを返します。

廃止予定: Titleが単語の境界を処理する際、Unicodeの句読点を適切に扱えません。golang.org/x/text/casesを代わりに使用してください。

Example
package main

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

func main() {
	fmt.Printf("%s", bytes.Title([]byte("her royal highness")))
}
Output:

Her Royal Highness

func ToLower

func ToLower(s []byte) []byte

ToLowerは、すべてのUnicodeの文字を小文字にマッピングしたバイトスライスsのコピーを返します。

Example
package main

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

func main() {
	fmt.Printf("%s", bytes.ToLower([]byte("Gopher")))
}
Output:

gopher

func ToLowerSpecial

func ToLowerSpecial(c unicode.SpecialCase, s []byte) []byte

ToLowerSpecialはUTF-8エンコードされたバイト列sを扱い、ユニコードの文字をすべて小文字に変換し、特殊なケースのルールを優先します。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/unicode"
)

func main() {
	str := []byte("AHOJ VÝVOJÁRİ GOLANG")
	totitle := bytes.ToLowerSpecial(unicode.AzeriCase, str)
	fmt.Println("Original : " + string(str))
	fmt.Println("ToLower : " + string(totitle))
}
Output:

Original : AHOJ VÝVOJÁRİ GOLANG
ToLower : ahoj vývojári golang

func ToTitle

func ToTitle(s []byte) []byte

ToTitleはsをUTF-8でエンコードされたバイト列として扱い、すべてのUnicodeの文字をタイトルケースにマップしたコピーを返します。

Example
package main

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

func main() {
	fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises")))
	fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб")))
}
Output:

LOUD NOISES
ХЛЕБ

func ToTitleSpecial

func ToTitleSpecial(c unicode.SpecialCase, s []byte) []byte

ToTitleSpecialはUTF-8でエンコードされたバイト列としてsを扱い、すべてのUnicode文字をタイトルケースにマッピングしたコピーを返します。特殊なケースのルールに優先します。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/unicode"
)

func main() {
	str := []byte("ahoj vývojári golang")
	totitle := bytes.ToTitleSpecial(unicode.AzeriCase, str)
	fmt.Println("Original : " + string(str))
	fmt.Println("ToTitle : " + string(totitle))
}
Output:

Original : ahoj vývojári golang
ToTitle : AHOJ VÝVOJÁRİ GOLANG

func ToUpper

func ToUpper(s []byte) []byte

ToUpperは、すべてのUnicode文字を大文字に変換したバイトスライスsのコピーを返します。

Example
package main

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

func main() {
	fmt.Printf("%s", bytes.ToUpper([]byte("Gopher")))
}
Output:

GOPHER

func ToUpperSpecial

func ToUpperSpecial(c unicode.SpecialCase, s []byte) []byte

ToUpperSpecialはsをUTF-8エンコードされたバイトとして扱い、すべてのUnicodeの文字をその大文字に変換したコピーを返します。特殊な大文字変換ルールを優先します。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/unicode"
)

func main() {
	str := []byte("ahoj vývojári golang")
	totitle := bytes.ToUpperSpecial(unicode.AzeriCase, str)
	fmt.Println("Original : " + string(str))
	fmt.Println("ToUpper : " + string(totitle))
}
Output:

Original : ahoj vývojári golang
ToUpper : AHOJ VÝVOJÁRİ GOLANG

func ToValidUTF8 added in v1.13.0

func ToValidUTF8(s, replacement []byte) []byte

ToValidUTF8は、sをUTF-8でエンコードされたバイトとして処理し、各バイトの連続が不正なUTF-8を表す場合に、置換バイト(空の場合もあります)で置き換えられたコピーを返します。

Example
package main

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

func main() {
	fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("abc"), []byte("\uFFFD")))
	fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("a\xffb\xC0\xAFc\xff"), []byte("")))
	fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("\xed\xa0\x80"), []byte("abc")))
}
Output:

abc
abc
abc

func Trim

func Trim(s []byte, cutset string) []byte

Trimは、cutsetに含まれるすべての先頭と末尾のUTF-8エンコードされたコードポイントをスライスして、sのサブスライスを返します。

Example
package main

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

func main() {
	fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! "))
}
Output:

["Achtung! Achtung"]

func TrimFunc

func TrimFunc(s []byte, f func(r rune) bool) []byte

TrimFunc は、前方および後方のすべての先頭と末尾をスライスして、f(c) で指定された条件を満たすすべての UTF-8 エンコードされたコードポイント c を除去して、s のサブスライスを返します。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/unicode"
)

func main() {
	fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter)))
	fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter)))
	fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct)))
	fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
}
Output:

-gopher!
"go-gopher!"
go-gopher
go-gopher!

func TrimLeft

func TrimLeft(s []byte, cutset string) []byte

TrimLeftは、cutsetに含まれるすべての先行するUTF-8エンコードされたコードポイントを除去することによって、sの一部のサブスライスを返します。

Example
package main

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

func main() {
	fmt.Print(string(bytes.TrimLeft([]byte("453gopher8257"), "0123456789")))
}
Output:

gopher8257

func TrimLeftFunc

func TrimLeftFunc(s []byte, f func(r rune) bool) []byte

TrimLeftFuncはUTF-8でエンコードされたバイト列sを処理し、f(c)を満たすすべての先頭のUTF-8エンコードされたコードポイントcを除いたsのサブスライスを返します。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/unicode"
)

func main() {
	fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter)))
	fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct)))
	fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
}
Output:

-gopher
go-gopher!
go-gopher!567

func TrimPrefix added in v1.1.0

func TrimPrefix(s, prefix []byte) []byte

TrimPrefixは、指定されたプレフィックス文字列を先頭から除去したsを返します。 sがプレフィックスで始まらない場合、sは変更されずに返されます。

Example
package main

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

func main() {
	var b = []byte("Goodbye,, world!")
	b = bytes.TrimPrefix(b, []byte("Goodbye,"))
	b = bytes.TrimPrefix(b, []byte("See ya,"))
	fmt.Printf("Hello%s", b)
}
Output:

Hello, world!

func TrimRight

func TrimRight(s []byte, cutset string) []byte

TrimRightは、cutsetに含まれるすべての末尾のUTF-8エンコードされたコードポイントを取り除いて、sの一部をサブスライスとして返します。

Example
package main

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

func main() {
	fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789")))
}
Output:

453gopher

func TrimRightFunc

func TrimRightFunc(s []byte, f func(r rune) bool) []byte

TrimRightFuncは、末尾にあるすべてのトレイリングUTF-8エンコードされたコードポイントcをスライスして、f(c)を満たすものを取り除いたsの部分スライスを返します。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/unicode"
)

func main() {
	fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter)))
	fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct)))
	fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
}
Output:

go-
go-gopher
1234go-gopher!

func TrimSpace

func TrimSpace(s []byte) []byte

TrimSpaceは、Unicodeで定義されたように、sの先頭と末尾のすべての空白を取り除いた部分列を返します。

Example
package main

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

func main() {
	fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n")))
}
Output:

a lone gopher

func TrimSuffix added in v1.1.0

func TrimSuffix(s, suffix []byte) []byte

TrimSuffixは、指定された末尾の接尾辞文字列を除いたsを返します。 もしsが接尾辞で終わっていない場合はsは変更されずにそのまま返されます。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/os"
)

func main() {
	var b = []byte("Hello, goodbye, etc!")
	b = bytes.TrimSuffix(b, []byte("goodbye, etc!"))
	b = bytes.TrimSuffix(b, []byte("gopher"))
	b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...)
	os.Stdout.Write(b)
}
Output:

Hello, world!

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Bufferはバイトの可変サイズのバッファであり、ReadとWriteのメソッドを持っています。 Bufferのゼロ値は、使い-readyになった空のバッファです。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/os"
)

func main() {
	var b bytes.Buffer // A Buffer needs no initialization.
	b.Write([]byte("Hello "))
	fmt.Fprintf(&b, "world!")
	b.WriteTo(os.Stdout)
}
Output:

Hello world!
Example (Reader)
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/encoding/base64"
	"github.com/shogo82148/std/io"
	"github.com/shogo82148/std/os"
)

func main() {
	// A Buffer can turn a string or a []byte into an io.Reader.
	buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==")
	dec := base64.NewDecoder(base64.StdEncoding, buf)
	io.Copy(os.Stdout, dec)
}
Output:

Gophers rule!

func NewBuffer

func NewBuffer(buf []byte) *Buffer

NewBufferは、bufを初期コンテンツとして使用して新しいBufferを作成および初期化します。 新しいBufferは、bufを所有し、この呼び出しの後にbufを使用しないようにする必要があります。 NewBufferは、既存のデータを読むためにBufferを準備するためのものです。書き込み用の内部バッファの初期サイズを設定するためにも使用できます。そのためには、 bufは希望する容量を持つ必要がありますが、長さはゼロである必要があります。

ほとんどの場合、new(Buffer)(または単にBuffer変数を宣言する)で Bufferを初期化するのに十分です。

func NewBufferString

func NewBufferString(s string) *Buffer

NewBufferStringは、文字列sを初期内容として使用して新しいBufferを作成し、初期化します。既存の文字列を読むためのバッファを準備するために使用されます。

ほとんどの場合、new(Buffer)(または単にBuffer変数を宣言する)でBufferを初期化するのに十分です。

func (*Buffer) Available added in v1.21.0

func (b *Buffer) Available() int

Availableはバッファ内で未使用のバイト数を返します。

func (*Buffer) AvailableBuffer added in v1.21.0

func (b *Buffer) AvailableBuffer() []byte

AvailableBufferはb.Available()の容量を持つ空のバッファを返します。 このバッファは追加され、直後のWrite呼び出しに渡すことが想定されています。 バッファはbに対する次の書き込み操作までの間のみ有効です。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/os"
	"github.com/shogo82148/std/strconv"
)

func main() {
	var buf bytes.Buffer
	for i := 0; i < 4; i++ {
		b := buf.AvailableBuffer()
		b = strconv.AppendInt(b, int64(i), 10)
		b = append(b, ' ')
		buf.Write(b)
	}
	os.Stdout.Write(buf.Bytes())
}
Output:

0 1 2 3

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytesは、バッファの未読部分を保持する長さb.Len()のスライスを返します。 このスライスは、次のバッファの変更までしか有効ではありません(つまり、 Read、Write、Reset、またはTruncateなどのメソッドの次の呼び出しまでのみ有効です)。 スライスは、次のバッファの変更まで少なくともバッファの内容をエイリアスしているため、 スライスへの直接の変更は将来の読み取り結果に影響を与えます。

Example
package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/os"
)

func main() {
	buf := bytes.Buffer{}
	buf.Write([]byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'})
	os.Stdout.Write(buf.Bytes())
}
Output:

hello world

func (*Buffer) Cap added in v1.5.0

func (b *Buffer) Cap() int

Capはバッファの基礎となるバイトスライスの容量、つまりバッファのデータのために割り当てられた総スペースを返します。

Example
package main

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

func main() {
	buf1 := bytes.NewBuffer(make([]byte, 10))
	buf2 := bytes.NewBuffer(make([]byte, 0, 10))
	fmt.Println(buf1.Cap())
	fmt.Println(buf2.Cap())
}
Output:

10
10

func (*Buffer) Grow added in v1.1.0

func (b *Buffer) Grow(n int)

Growは必要に応じてバッファの容量を増やし、残りのnバイトの空間を保証します。Grow(n)の後、バッファには少なくともnバイトを別の割り当てなしで書き込むことができます。 nが負数の場合、Growはパニックを引き起こします。 バッファを拡大できない場合、ErrTooLargeでパニックを引き起こします。

Example
package main

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

func main() {
	var b bytes.Buffer
	b.Grow(64)
	bb := b.Bytes()
	b.Write([]byte("64 bytes or fewer"))
	fmt.Printf("%q", bb[:b.Len()])
}
Output:

"64 bytes or fewer"

func (*Buffer) Len

func (b *Buffer) Len() int

Lenはバッファの未読部分のバイト数を返します。 b.Len() == len(b.Bytes())。

Example
package main

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

func main() {
	var b bytes.Buffer
	b.Grow(64)
	b.Write([]byte("abcde"))
	fmt.Printf("%d", b.Len())
}
Output:

5

func (*Buffer) Next

func (b *Buffer) Next(n int) []byte

Nextは、バッファから次のnバイトを含むスライスを返し、 バイトがReadによって返された場合と同様にバッファを進めます。 バッファにnバイト未満のバイトがある場合、Nextはバッファ全体を返します。 スライスは、次の読み取りまたは書き込みメソッドの呼び出しまでの間のみ有効です。

Example
package main

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

func main() {
	var b bytes.Buffer
	b.Grow(64)
	b.Write([]byte("abcde"))
	fmt.Printf("%s\n", string(b.Next(2)))
	fmt.Printf("%s\n", string(b.Next(2)))
	fmt.Printf("%s", string(b.Next(2)))
}
Output:

ab
cd
e

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (n int, err error)

Readは、バッファから次のlen(p)バイトを読み取るか、バッファが空になるまで読み取ります。返り値nは読み取られたバイト数です。バッファに返すデータがない場合、errはio.EOFです(len(p)がゼロの場合を除く);それ以外の場合、nilです。

Example
package main

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

func main() {
	var b bytes.Buffer
	b.Grow(64)
	b.Write([]byte("abcde"))
	rdbuf := make([]byte, 1)
	n, err := b.Read(rdbuf)
	if err != nil {
		panic(err)
	}
	fmt.Println(n)
	fmt.Println(b.String())
	fmt.Println(string(rdbuf))
	// Output
	// 1
	// bcde
	// a
}
Output:

func (*Buffer) ReadByte

func (b *Buffer) ReadByte() (byte, error)

ReadByte はバッファから次のバイトを読み込んで返します。 バイトが利用できない場合は、エラー io.EOF を返します。

Example
package main

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

func main() {
	var b bytes.Buffer
	b.Grow(64)
	b.Write([]byte("abcde"))
	c, err := b.ReadByte()
	if err != nil {
		panic(err)
	}
	fmt.Println(c)
	fmt.Println(b.String())
	// Output
	// 97
	// bcde
}
Output:

func (*Buffer) ReadBytes

func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)

ReadBytesは入力の最初のdelimが現れるまで読み取り、 デリミタを含むデータを含むスライスを返します。 ReadBytesがデリミタを見つける前にエラーに遭遇した場合、 エラー自体(しばしばio.EOF)とエラー前に読み取ったデータを返します。 返されたデータの末尾がdelimで終わっていない場合、 ReadBytesはerr != nilを返します。

func (*Buffer) ReadFrom

func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)

ReadFromは、rからEOFまでデータを読み取り、バッファに追加していきます。必要に応じてバッファのサイズが拡大されます。返り値nは読み取られたバイト数です。読み取り中にio.EOF以外のエラーが発生した場合、それも返されます。バッファがあまりに大きくなると、ReadFromはErrTooLargeでパニックを引き起こします。

func (*Buffer) ReadRune

func (b *Buffer) ReadRune() (r rune, size int, err error)

ReadRuneはバッファから次のUTF-8エンコードされた Unicodeコードポイントを読み取り、返します。 バイトが利用できない場合は、io.EOFというエラーが返されます。 バイトが不正なUTF-8エンコーディングの場合、1バイトを消費し、U+FFFD、1を返します。

func (*Buffer) ReadString

func (b *Buffer) ReadString(delim byte) (line string, err error)

ReadStringは入力の最初のデリミタが現れるまで読み取り、 デリミタを含むデータを含む文字列を返します。 ReadStringがデリミタを見つける前にエラーに遭遇する場合、 エラー自体(通常はio.EOF)とエラーが発生する前に読み取ったデータを返します。 ReadStringは、返されるデータがdelimで終わっていない場合、err!= nilを返します。

func (*Buffer) Reset

func (b *Buffer) Reset()

Resetはバッファを空にリセットしますが、将来の書き込みのために基礎となるストレージは保持されます。 ResetはTruncate(0)と同じです。

func (*Buffer) String

func (b *Buffer) String() string

Stringはバッファの未読部分の内容を文字列として返します。もしBufferがnilポインタであれば、"<nil>"を返します。

より効率的に文字列を構築するには、strings.Builder型を参照してください。

func (*Buffer) Truncate

func (b *Buffer) Truncate(n int)

Truncateはバッファから最初のnバイト以外の未読データを削除し、同じ割り当てられたストレージを使用し続けます。 nが負数またはバッファの長さよりも大きい場合、パニックが発生します。

func (*Buffer) UnreadByte

func (b *Buffer) UnreadByte() error

UnreadByteは、少なくとも1バイトを読み込んだ最後の成功した読み込み操作で返された最後のバイトを戻します。最後の読み込み以降に書き込みが発生した場合、最後の読み込みがエラーを返した場合、または読み込みが0バイトを読み込んだ場合、UnreadByteはエラーを返します。

func (*Buffer) UnreadRune

func (b *Buffer) UnreadRune() error

UnreadRuneはReadRuneによって返された最後のルーンを未読状態にします。 バッファ上の直近の読み込みや書き込み操作が成功していない場合、UnreadRuneはエラーを返します。 (この点で、UnreadByteよりも厳格です。UnreadByteはすべての読み込み操作から最後のバイトを未読状態にします。)

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

Write はバッファーに p の内容を追加し、必要に応じてバッファーを拡張します。戻り値 n は p の長さであり、err は常に nil です。バッファーが大きすぎる場合、Write は ErrTooLarge とともにパニックを発生させます。

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(c byte) error

WriteByteはバイトcをバッファに追加し、必要に応じてバッファを拡張します。 返されるエラーは常にnilですが、bufio.WriterのWriteByteに合わせるために含まれています。 バッファが大きすぎる場合、WriteByteはErrTooLargeでパニックします。

func (*Buffer) WriteRune

func (b *Buffer) WriteRune(r rune) (n int, err error)

WriteRuneはUnicodeコードポイントrのUTF-8エンコーディングをバッファに追加し、その長さと常にnilであるエラーを返します。エラーは常にnilですが、bufio.WriterのWriteRuneとのマッチングのために含まれます。必要に応じてバッファは拡張されます。もしバッファがあまりにも大きくなった場合、WriteRuneはErrTooLargeでパニックを起こします。

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string) (n int, err error)

WriteStringは、必要に応じてバッファを拡張し、sの内容をバッファに追加します。戻り値nはsの長さであり、errは常にnilです。バッファが大きすぎる場合、WriteStringはErrTooLargeとともにパニックを発生させます。

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)

WriteTo はバッファが空になるかエラーが発生するまで、データを w に書き込みます。 戻り値の n は書き込まれたバイト数です。この値は常に int に収まりますが、io.WriterTo インターフェースに合わせて int64 型です。書き込み中に発生したエラーも返されます。

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Readerはio.Reader、io.ReaderAt、io.WriterTo、io.Seeker、io.ByteScanner、io.RuneScannerのインターフェースを実装し、 バイトスライスから読み取ります。 Bufferとは異なり、Readerは読み込み専用であり、シークをサポートします。 Readerのゼロ値は空のスライスのReaderのように動作します。

func NewReader

func NewReader(b []byte) *Reader

NewReaderはbから読み込む新しいReaderを返す。

func (*Reader) Len

func (r *Reader) Len() int

Lenはスライスの未読部分のバイト数を返します。

Example
package main

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

func main() {
	fmt.Println(bytes.NewReader([]byte("Hi!")).Len())
	fmt.Println(bytes.NewReader([]byte("こんにちは!")).Len())
}
Output:

3
16

func (*Reader) Read

func (r *Reader) Read(b []byte) (n int, err error)

Readはio.Readerインターフェースを実装します。

func (*Reader) ReadAt

func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)

ReadAtはio.ReaderAtインターフェースを実装します。

func (*Reader) ReadByte

func (r *Reader) ReadByte() (byte, error)

ReadByteはio.ByteReaderインターフェースを実装します。

func (*Reader) ReadRune

func (r *Reader) ReadRune() (ch rune, size int, err error)

ReadRuneはio.RuneReaderインターフェースを実装します。

func (*Reader) Reset added in v1.7.0

func (r *Reader) Reset(b []byte)

ResetはReaderをbからの読み取りにリセットします。

func (*Reader) Seek

func (r *Reader) Seek(offset int64, whence int) (int64, error)

Seek は io.Seeker インターフェースを実装します。

func (*Reader) Size added in v1.5.0

func (r *Reader) Size() int64

Size は元のバイトスライスの長さを返します。 Size は ReadAt を通じて読み取り可能なバイト数です。 Reset を除くすべてのメソッド呼び出しによって結果は変わりません。

func (*Reader) UnreadByte

func (r *Reader) UnreadByte() error

UnreadByteはio.ByteScannerインターフェースを実装する際にReadByteを補完します。

func (*Reader) UnreadRune

func (r *Reader) UnreadRune() error

UnreadRuneはio.RuneScannerインターフェースの実装においてReadRuneを補完します。

func (*Reader) WriteTo added in v1.1.0

func (r *Reader) WriteTo(w io.Writer) (n int64, err error)

WriteToはio.WriterToインターフェースを実装します。

Jump to

Keyboard shortcuts

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