package ascii
import "git.sr.ht/~shulhan/pakakeh.go/lib/ascii"
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 MakeSet(chars string) (as Set, ok bool)
- 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.
func IsAlpha ¶
IsAlpha will return true if byte is ASCII alphabet character, otherwise it will return false.
func IsDigit ¶
IsDigit will return true if byte is ASCII digit, otherwise it will return false.
func IsDigits ¶
IsDigits will return true if all bytes are ASCII digit, otherwise it will
return false.
Code:play
Output: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)))
}
}
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.
func IsSpace ¶
IsSpace will return true if byte is ASCII white spaces character,
otherwise it will return false.
Code:play
Output: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'))
}
\t: true
\n: true
\v: true
\f: true
\r: true
: true
: true
\: false
0: false
func Random ¶
Random generate random sequence of value from source with fixed length.
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.
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.
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) Add ¶
Add inserts character c into the set.
func (*Set) Contains ¶
Contains reports whether c is inside the set.
func (*Set) Equal ¶
Equal reports whether as contains the same characters as as2.
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) Size ¶
Size returns the number of characters in the set.
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.
Source Files ¶
- Version
- v0.60.0 (latest)
- Published
- Feb 1, 2025
- Platform
- linux/amd64
- Imports
- 4 packages
- Last checked
- 9 hours ago –
Tools for package owners.