package runes
import "git.sr.ht/~shulhan/pakakeh.go/lib/runes"
Package runes provide a library for working with a single rune or slice of rune.
Index ¶
- func Contain(s []rune, c rune) (bool, int)
- func Diff(l []rune, r []rune) (diff []rune)
- func EncloseRemove(line, leftcap, rightcap []rune) ([]rune, bool)
- func FindSpace(line []rune, startAt int) (idx int)
- func Inverse(in []rune) []rune
- func TokenFind(line, token []rune, startAt int) (at int)
Examples ¶
Functions ¶
func Contain ¶
Contain find a rune `c` inside `s`.
If `c` found in `s` it will return boolean true and index of `c` in `s`;
otherwise it will return false and -1.
Code:
Output:Example¶
{
line := []rune(`a b c`)
found, idx := Contain(line, 'a')
fmt.Printf("%t %d\n", found, idx)
found, idx = Contain(line, 'x')
fmt.Printf("%t %d\n", found, idx)
// Output:
// true 0
// false -1
}
true 0
false -1
func Diff ¶
Diff return the difference between two slice of rune.
Code:
Output:Example¶
{
l := []rune{'a', 'b', 'c', 'd'}
r := []rune{'b', 'c'}
fmt.Printf("%c\n", Diff(l, r))
// Output: [a d]
}
[a d]
func EncloseRemove ¶
EncloseRemove given a line, remove all characters inside it, starting from `leftcap` until the `rightcap` and return cutted line and changed to true.
If no `leftcap` or `rightcap` is found, the line will unchanged, and
returned status will be false.
Code:
Output:Example¶
{
line := []rune(`[[ ABC ]] DEF`)
leftcap := []rune(`[[`)
rightcap := []rune(`]]`)
got, changed := EncloseRemove(line, leftcap, rightcap)
fmt.Printf("'%s' %t\n", string(got), changed)
// Output: ' DEF' true
}
' DEF' true
func FindSpace ¶
FindSpace find any unicode spaces in line start from index `startAt` and
return their index.
If no spaces found it will return -1.
Code:
Output:Example¶
{
line := []rune(`Find a space`)
fmt.Printf("%d\n", FindSpace(line, 0))
fmt.Printf("%d\n", FindSpace(line, 5))
// Output:
// 4
// 6
}
4
6
func Inverse ¶
Inverse the input slice of rune with inplace reversion (without allocating
another slice).
Code:
Output:Example¶
{
fmt.Printf("%s\n", string(Inverse([]rune(``))))
fmt.Printf("%s\n", string(Inverse([]rune(`a`))))
fmt.Printf("%s\n", string(Inverse([]rune(`ab`))))
fmt.Printf("%s\n", string(Inverse([]rune(`abc`))))
fmt.Printf("%s\n", string(Inverse([]rune(`abcd`))))
fmt.Printf("%s\n", string(Inverse([]rune(`abcde`))))
// Output:
//
// a
// ba
// cba
// dcba
// edcba
}
a
ba
cba
dcba
edcba
func TokenFind ¶
TokenFind will search token in text starting from index `startAt` and return the position where the match start.
If no token is found it will return -1.
Code:
Output:Example¶
{
line := []rune("// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.")
token := []rune("right")
at := TokenFind(line, token, 0)
fmt.Printf("%d\n", at)
// Output: 7
}
7
Source Files ¶
- Version
- v0.60.0 (latest)
- Published
- Feb 1, 2025
- Platform
- linux/amd64
- Imports
- 1 packages
- Last checked
- 6 minutes ago –
Tools for package owners.