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

Examples

Functions

func Contain

func Contain(s []rune, c rune) (bool, int)

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.

Example

Code:

{
	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
}

Output:

true 0
false -1

func Diff

func Diff(l []rune, r []rune) (diff []rune)

Diff return the difference between two slice of rune.

Example

Code:

{
	l := []rune{'a', 'b', 'c', 'd'}
	r := []rune{'b', 'c'}
	fmt.Printf("%c\n", Diff(l, r))
	// Output: [a d]
}

Output:

[a d]

func EncloseRemove

func EncloseRemove(line, leftcap, rightcap []rune) ([]rune, bool)

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.

Example

Code:

{
	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
}

Output:

' DEF' true

func FindSpace

func FindSpace(line []rune, startAt int) (idx int)

FindSpace find any unicode spaces in line start from index `startAt` and return their index. If no spaces found it will return -1.

Example

Code:

{
	line := []rune(`Find a space`)
	fmt.Printf("%d\n", FindSpace(line, 0))
	fmt.Printf("%d\n", FindSpace(line, 5))
	// Output:
	// 4
	// 6
}

Output:

4
6

func Inverse

func Inverse(in []rune) []rune

Inverse the input slice of rune with inplace reversion (without allocating another slice).

Example

Code:

{
	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
}

Output:

a
ba
cba
dcba
edcba

func TokenFind

func TokenFind(line, token []rune, startAt int) (at int)

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.

Example

Code:

{
	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
}

Output:

7

Source Files

runes.go

Version
v0.60.0 (latest)
Published
Feb 1, 2025
Platform
linux/amd64
Imports
1 packages
Last checked
6 minutes ago

Tools for package owners.