Documentation ¶
Overview ¶
Package ascii provide a library for working with ASCII characters.
Index ¶
- Constants
- Variables
- func IsAlnum(b byte) bool
- func IsAlpha(b byte) bool
- func IsDigit(b byte) bool
- func IsDigits(data []byte) bool
- func IsHex(b byte) bool
- func IsSpace(b byte) bool
- func Random(source []byte, n int) []byte
- func ToLower(data []byte) []byte
- func ToUpper(data []byte) []byte
- type Set
- func (as *Set) Add(c byte)
- func (as *Set) Contains(c byte) bool
- func (as *Set) Equal(as2 Set) bool
- func (as *Set) Intersection(as2 Set) (as3 Set)
- func (as *Set) Remove(c byte)
- func (as *Set) Size() int
- func (as *Set) Subtract(as2 Set) (as3 Set)
- func (as *Set) Union(as2 Set) (as3 Set)
- func (as *Set) Visit(do func(n byte) (skip bool)) (aborted bool)
Examples ¶
Constants ¶
const ( // Letters contains list of lower and upper case characters in ASCII. Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // LettersNumber contains list of lower and upper case characters in // ASCII along with numbers. LettersNumber = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890" // HexaLETTERS contains list of hexadecimal characters in upper cases. HexaLETTERS = "0123456789ABCDEF" // HexaLetters contains list of hexadecimal characters in lower and // upper cases. HexaLetters = "0123456789abcedfABCDEF" // Hexaletters contains list of hexadecimal characters in lower cases. Hexaletters = "0123456789abcedf" )
Variables ¶
var ( // Spaces contains list of white spaces in ASCII. Spaces = []byte{'\t', '\n', '\v', '\f', '\r', ' '} )
Functions ¶
func IsAlnum ¶
IsAlnum will return true if byte is ASCII alphanumeric character, otherwise it will return false.
Example ¶
package main import ( "fmt" "git.sr.ht/~shulhan/pakakeh.go/lib/ascii" ) func main() { chars := []byte(`0aZ!.`) for _, c := range chars { fmt.Printf("%c: %t\n", c, ascii.IsAlnum(c)) } }
Output: 0: true a: true Z: true !: false .: false
func IsAlpha ¶
IsAlpha will return true if byte is ASCII alphabet character, otherwise it will return false.
Example ¶
package main import ( "fmt" "git.sr.ht/~shulhan/pakakeh.go/lib/ascii" ) func main() { chars := []byte(`0aZ!.`) for _, c := range chars { fmt.Printf("%c: %t\n", c, ascii.IsAlpha(c)) } }
Output: 0: false a: true Z: true !: false .: false
func IsDigit ¶
IsDigit will return true if byte is ASCII digit, otherwise it will return false.
Example ¶
package main import ( "fmt" "git.sr.ht/~shulhan/pakakeh.go/lib/ascii" ) func main() { chars := []byte(`0aZ!.`) for _, c := range chars { fmt.Printf("%c: %t\n", c, ascii.IsDigit(c)) } }
Output: 0: true a: false Z: false !: false .: false
func IsDigits ¶
IsDigits will return true if all bytes are ASCII digit, otherwise it will return false.
Example ¶
package main import ( "fmt" "git.sr.ht/~shulhan/pakakeh.go/lib/ascii" ) func main() { inputs := []string{ `012`, `012 `, ` 012 `, `0.`, `0.1`, `0.1a`, } for _, s := range inputs { fmt.Printf("%s: %t\n", s, ascii.IsDigits([]byte(s))) } }
Output: 012: true 012 : false 012 : false 0.: false 0.1: false 0.1a: false
func IsHex ¶
IsHex will return true if byte is hexadecimal number, otherwise it will return false.
Example ¶
package main import ( "fmt" "git.sr.ht/~shulhan/pakakeh.go/lib/ascii" ) func main() { chars := []byte(`09afgAFG`) for _, c := range chars { fmt.Printf("%c: %t\n", c, ascii.IsHex(c)) } }
Output: 0: true 9: true a: true f: true g: false A: true F: true G: false
func IsSpace ¶
IsSpace will return true if byte is ASCII white spaces character, otherwise it will return false.
Example ¶
package main import ( "fmt" "git.sr.ht/~shulhan/pakakeh.go/lib/ascii" ) func main() { fmt.Printf("\\t: %t\n", ascii.IsSpace('\t')) fmt.Printf("\\n: %t\n", ascii.IsSpace('\n')) fmt.Printf("\\v: %t\n", ascii.IsSpace('\v')) fmt.Printf("\\f: %t\n", ascii.IsSpace('\f')) fmt.Printf("\\r: %t\n", ascii.IsSpace('\r')) fmt.Printf(" : %t\n", ascii.IsSpace(' ')) fmt.Printf(" : %t\n", ascii.IsSpace(' ')) fmt.Printf("\\: %t\n", ascii.IsSpace('\\')) fmt.Printf("0: %t\n", ascii.IsSpace('0')) }
Output: \t: true \n: true \v: true \f: true \r: true : true : true \: false 0: false
func ToLower ¶
ToLower convert slice of ASCII characters to lower cases, in places, which means it will return the same slice instead of creating new one.
Example ¶
package main import ( "fmt" "git.sr.ht/~shulhan/pakakeh.go/lib/ascii" ) func main() { in := []byte(`@ABCDEFGhijklmnoPQRSTUVWxyz{12345678`) fmt.Printf("%s\n", ascii.ToLower(in)) }
Output: @abcdefghijklmnopqrstuvwxyz{12345678
func ToUpper ¶
ToUpper convert slice of ASCII characters to upper cases, in places, which means it will return the same slice instead of creating new one.
Example ¶
package main import ( "fmt" "git.sr.ht/~shulhan/pakakeh.go/lib/ascii" ) func main() { in := []byte(`@ABCDEFGhijklmnoPQRSTUVWxyz{12345678`) fmt.Printf("%s\n", ascii.ToUpper(in)) }
Output: @ABCDEFGHIJKLMNOPQRSTUVWXYZ{12345678
Types ¶
type Set ¶
type Set [9]uint32
Set is a 36-byte value, where each bit in the first 32-bytes represents the presence of a given ASCII character in the set. The remaining 4-bytes is a counter for the number of ASCII characters in the set. The 128-bits of the first 16 bytes, starting with the least-significant bit of the lowest word to the most-significant bit of the highest word, map to the full range of all 128 ASCII characters. The 128-bits of the next 16 bytes will be zeroed, ensuring that any non-ASCII character will be reported as not in the set. Rejecting non-ASCII characters in this way avoids bounds checks in Set.Contains.
func MakeSet ¶
MakeSet creates a set of ASCII characters and reports whether all characters in chars are ASCII.
func (*Set) Intersection ¶
Intersection returns a new set containing all characters that belong to both as and as2.
func (*Set) Remove ¶
Remove removes c from the set
if c is not in the set, the set contents will remain unchanged.
func (*Set) Subtract ¶
Subtract returns a new set containing all characters that belong to as but not as2.
func (*Set) Union ¶
Union returns a new set containing all characters that belong to either as and as2.
func (*Set) Visit ¶
Visit calls the do function for each character of as in ascending numerical order. If do returns true, Visit returns immediately, skipping any remaining characters, and returns true. It is safe for do to Add or Remove characters. The behavior of Visit is undefined if do changes the set in any other way.