ascii

package
v0.53.1 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: BSD-3-Clause Imports: 4 Imported by: 1

Documentation

Overview

Package ascii provide a library for working with ASCII characters.

Index

Examples

Constants

View Source
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

View Source
var (
	// Spaces contains list of white spaces in ASCII.
	Spaces = []byte{'\t', '\n', '\v', '\f', '\r', ' '}
)

Functions

func IsAlnum

func IsAlnum(b byte) bool

IsAlnum will return true if byte is ASCII alphanumeric character, otherwise it will return false.

Example
package main

import (
	"fmt"

	"github.com/shuLhan/share/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

func IsAlpha(b byte) bool

IsAlpha will return true if byte is ASCII alphabet character, otherwise it will return false.

Example
package main

import (
	"fmt"

	"github.com/shuLhan/share/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

func IsDigit(b byte) bool

IsDigit will return true if byte is ASCII digit, otherwise it will return false.

Example
package main

import (
	"fmt"

	"github.com/shuLhan/share/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

func IsDigits(data []byte) bool

IsDigits will return true if all bytes are ASCII digit, otherwise it will return false.

Example
package main

import (
	"fmt"

	"github.com/shuLhan/share/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

func IsHex(b byte) bool

IsHex will return true if byte is hexadecimal number, otherwise it will return false.

Example
package main

import (
	"fmt"

	"github.com/shuLhan/share/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

func IsSpace(b byte) bool

IsSpace will return true if byte is ASCII white spaces character, otherwise it will return false.

Example
package main

import (
	"fmt"

	"github.com/shuLhan/share/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 Random

func Random(source []byte, n int) []byte

Random generate random sequence of value from source with fixed length.

func ToLower

func ToLower(data []byte) []byte

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"

	"github.com/shuLhan/share/lib/ascii"
)

func main() {
	in := []byte(`@ABCDEFGhijklmnoPQRSTUVWxyz{12345678`)
	fmt.Printf("%s\n", ascii.ToLower(in))
}
Output:

@abcdefghijklmnopqrstuvwxyz{12345678

func ToUpper

func ToUpper(data []byte) []byte

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"

	"github.com/shuLhan/share/lib/ascii"
)

func main() {
	in := []byte(`@ABCDEFGhijklmnoPQRSTUVWxyz{12345678`)
	fmt.Printf("%s\n", ascii.ToUpper(in))
}
Output:

@ABCDEFGHIJKLMNOPQRSTUVWXYZ{12345678

Types

type Set added in v0.48.0

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 added in v0.48.0

func MakeSet(chars string) (as Set, ok bool)

MakeSet creates a set of ASCII characters and reports whether all characters in chars are ASCII.

func (*Set) Add added in v0.48.0

func (as *Set) Add(c byte)

Add inserts character c into the set.

func (*Set) Contains added in v0.48.0

func (as *Set) Contains(c byte) bool

Contains reports whether c is inside the set.

func (*Set) Equal added in v0.48.0

func (as *Set) Equal(as2 Set) bool

Equal reports whether as contains the same characters as as2.

func (*Set) Intersection added in v0.48.0

func (as *Set) Intersection(as2 Set) (as3 Set)

Intersection returns a new set containing all characters that belong to both as and as2.

func (*Set) Remove added in v0.48.0

func (as *Set) Remove(c byte)

Remove removes c from the set

if c is not in the set, the set contents will remain unchanged.

func (*Set) Size added in v0.48.0

func (as *Set) Size() int

Size returns the number of characters in the set.

func (*Set) Subtract added in v0.48.0

func (as *Set) Subtract(as2 Set) (as3 Set)

Subtract returns a new set containing all characters that belong to as but not as2.

func (*Set) Union added in v0.48.0

func (as *Set) Union(as2 Set) (as3 Set)

Union returns a new set containing all characters that belong to either as and as2.

func (*Set) Visit added in v0.48.0

func (as *Set) Visit(do func(n byte) (skip bool)) (aborted bool)

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.

Jump to

Keyboard shortcuts

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