runes

package
v0.47.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2023 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package runes provide a library for working with a single rune or slice of rune.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

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

func Diff

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

Diff return the difference between two slice of rune.

Example
l := []rune{'a', 'b', 'c', 'd'}
r := []rune{'b', 'c'}
fmt.Printf("%c\n", Diff(l, r))
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
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

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
line := []rune(`Find a space`)
fmt.Printf("%d\n", FindSpace(line, 0))
fmt.Printf("%d\n", FindSpace(line, 5))
Output:

4
6

func Inverse added in v0.23.0

func Inverse(in []rune) []rune

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

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

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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