utf8

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package utf8 implements functions and constants to support text encoded in UTF-8. It includes functions to translate between runes and UTF-8 byte sequences. See https://en.wikipedia.org/wiki/UTF-8

Index

Examples

Constants

View Source
const (
	RuneSelf     = 0x80 // characters below RuneSelf are represented as themselves in a single byte.
	RuneErrorLen = 3    // encoded UTF-8 length of a unicodeconst.RuneError
	MaxRuneLen   = 4    // maximum number of bytes of a UTF-8 encoded Unicode character.
)
View Source
const (
	Rune1Max = 1<<7 - 1
	Rune2Max = 1<<11 - 1
	Rune3Max = 1<<16 - 1
)

Variables

This section is empty.

Functions

func AppendRunes

func AppendRunes(dst []byte, src ...rune) []byte

AppendRunes appends the UTF-8 encoding of r to the end of p and returns the extended buffer. If the rune is out of range, it appends the encoding of RuneError.

Example
package main

import (
	"fmt"

	"github.com/primecitizens/pcz/std/text/unicode/utf8"
)

func main() {
	buf1 := utf8.AppendRunes(nil, 0x10000)
	buf2 := utf8.AppendRunes([]byte("init"), 0x10000)
	fmt.Println(string(buf1))
	fmt.Println(string(buf2))
}
Output:

𐀀
init𐀀

func Count

func Count(s string) (n int)

Count returns the number of runes in s. Erroneous and short encodings are treated as single runes of width 1 byte.

Example
package main

import (
	"fmt"

	"github.com/primecitizens/pcz/std/text/unicode/utf8"
)

func main() {
	str := "Hello, 世界"
	fmt.Println("bytes =", len(str))
	fmt.Println("runes =", utf8.Count(str))
}
Output:

bytes = 13
runes = 9

func EncodeRune

func EncodeRune(dst []byte, r rune) ([]byte, int)

EncodeRune writes into dst the UTF-8 encoding of the rune.

If the rune is out of range, it writes the encoding of RuneError.

It returns the number of bytes written, and when dst is too small to write the complete byte sequence of the rune, it returns 0.

Example
package main

import (
	"fmt"

	"github.com/primecitizens/pcz/std/text/unicode/utf8"
)

func main() {
	r := '世'
	buf := make([]byte, 0, 3)

	buf, n := utf8.EncodeRune(buf, r)

	fmt.Println(buf)
	fmt.Println(n)
}
Output:

[228 184 150]
3
Example (OutOfRange)
package main

import (
	"fmt"

	"github.com/primecitizens/pcz/std/text/unicode/common"
	"github.com/primecitizens/pcz/std/text/unicode/utf8"
)

func main() {
	runes := []rune{
		// Less than 0, out of range.
		-1,
		// Greater than 0x10FFFF, out of range.
		0x110000,
		// The Unicode replacement character.
		common.RuneError,
	}
	for i, c := range runes {
		buf := make([]byte, 0, 3)
		buf, size := utf8.EncodeRune(buf, c)
		fmt.Printf("%d: %d %[2]s %d\n", i, buf, size)
	}
}
Output:

0: [239 191 189] � 3
1: [239 191 189] � 3
2: [239 191 189] � 3

func EncodeRuneNonASCII

func EncodeRuneNonASCII(dst []byte, r rune) ([]byte, int)

func First

func First(s string) (r rune, size int)

First unpacks the first UTF-8 encoding in s and returns the rune and its width in bytes. If p is empty it returns (RuneError, 0).

Otherwise, if the encoding is invalid, it returns (RuneError, 1). Both are impossible results for correct, non-empty UTF-8.

An encoding is invalid if it is incorrect UTF-8, encodes a rune that is out of range, or is not the shortest possible UTF-8 encoding for the value. No other validation is performed.

Example
package main

import (
	"fmt"

	"github.com/primecitizens/pcz/std/text/unicode/utf8"
)

func main() {
	str := "Hello, 世界"

	for len(str) > 0 {
		r, size := utf8.First(str)
		fmt.Printf("%c %v\n", r, size)

		str = str[size:]
	}
}
Output:

H 1
e 1
l 1
l 1
o 1
, 1
  1
世 3
界 3

func FullRune

func FullRune(s string) bool

FullRune reports whether the bytes in s begin with a full UTF-8 encoding of a rune.

An invalid encoding is considered a full Rune since it will convert as a width-1 error rune.

Example
package main

import (
	"fmt"

	"github.com/primecitizens/pcz/std/text/unicode/utf8"
)

func main() {
	str := "世"
	fmt.Println(utf8.FullRune(str))
	fmt.Println(utf8.FullRune(str[:2]))
}
Output:

true
false

func IterRune

func IterRune(p string, pos int) (r rune, next int)

IterRune returns the rune at the start of p[pos:] and the index after the rune in p.

If the string appears to be incomplete or decoding problems are encountered (RuneError, pos+1) is returned to ensure progress when IterRune is used to iterate over a string.

func Last

func Last(s string) (r rune, size int)

Last unpacks the last UTF-8 encoding in p and returns the rune and its width in bytes. If p is empty it returns (RuneError, 0). Otherwise, if the encoding is invalid, it returns (RuneError, 1). Both are impossible results for correct, non-empty UTF-8.

An encoding is invalid if it is incorrect UTF-8, encodes a rune that is out of range, or is not the shortest possible UTF-8 encoding for the value. No other validation is performed.

Example
package main

import (
	"fmt"

	"github.com/primecitizens/pcz/std/text/unicode/utf8"
)

func main() {
	str := "Hello, 世界"

	for len(str) > 0 {
		r, size := utf8.Last(str)
		fmt.Printf("%c %v\n", r, size)

		str = str[:len(str)-size]
	}
}
Output:

界 3
世 3
  1
, 1
o 1
l 1
l 1
e 1
H 1

func RuneLen

func RuneLen(r rune) int

RuneLen returns the number of bytes required to encode the rune.

It returns 0 if the rune is not a valid value to encode in UTF-8.

Example
package main

import (
	"fmt"

	"github.com/primecitizens/pcz/std/text/unicode/utf8"
)

func main() {
	fmt.Println(utf8.RuneLen('a'))
	fmt.Println(utf8.RuneLen('界'))
}
Output:

1
3

func RuneLenNonASCII

func RuneLenNonASCII(r rune) int

func RuneStart

func RuneStart(b byte) bool

RuneStart reports whether the byte could be the first byte of an encoded, possibly invalid rune. Second and subsequent bytes always have the top two bits set to 10.

Example
package main

import (
	"fmt"

	"github.com/primecitizens/pcz/std/text/unicode/utf8"
)

func main() {
	buf := []byte("a界")
	fmt.Println(utf8.RuneStart(buf[0]))
	fmt.Println(utf8.RuneStart(buf[1]))
	fmt.Println(utf8.RuneStart(buf[2]))
}
Output:

true
true
false

func RuneValid

func RuneValid(r rune) bool

RuneValid reports whether r can be legally encoded as UTF-8. Code points that are out of range or a surrogate half are illegal.

Example
package main

import (
	"fmt"

	"github.com/primecitizens/pcz/std/text/unicode/utf8"
)

func main() {
	valid := 'a'
	invalid := rune(0xfffffff)

	fmt.Println(utf8.RuneValid(valid))
	fmt.Println(utf8.RuneValid(invalid))
}
Output:

true
false

func Valid

func Valid(s string) bool

Valid reports whether s consists entirely of valid UTF-8-encoded runes.

Example
package main

import (
	"fmt"

	"github.com/primecitizens/pcz/std/text/unicode/utf8"
)

func main() {
	valid := "Hello, 世界"
	invalid := string([]byte{0xff, 0xfe, 0xfd})

	fmt.Println(utf8.Valid(valid))
	fmt.Println(utf8.Valid(invalid))
}
Output:

true
false

Types

type String

type String string

func AsString

func AsString(s []byte) String

func (String) Slice

func (s String) Slice() []byte

Jump to

Keyboard shortcuts

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