rslice

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: MIT Imports: 2 Imported by: 0

README

rslice

test workflow Go Coverage Go Report Card Go Reference

Some operations for working with []rune thingies and stuff which may be particularly useful for things like justifying text, manipulating strings, or just being a dang hunk.

I mostly wrote this to lift it out of tooey

Some Example Usage


s := []rune("    TEST STRING")
s = ShiftWhiteSpaceRight(s)

> "TEST STRING     "

s := []rune("ABCDEF")
s = ShiftLeft(s)

> "BCDEFA"

See the library's tests for extensive example usages

Documentation

Overview

package rslice provides some common []rune patterns for moving runes around within a slice, justifying embedded text, etc

Charles <asciifaceman> Corbett 2023

MIT License

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LeastWhitespaceIndex

func LeastWhitespaceIndex(slice []rune) int

LeastWhitespaceIndex returns an index point of a []rune with the least whitespace between the left and right most characters

It will wait until it has passed at least one non-whitespace character before recording the potential index.

A return of -1 indicates there is no suitable index, an example string would be ` a ` which has no whitespace between two non-whitespace characters

Currently this function will trigger an ignore on whitespace after a control character is encountered until the next non-whitespace non-control character and effectively erase any whitespace between the previous non-ws/non-cc rune to prevent returning an index between or before a control character wh

func Newline

func Newline(r rune) bool

Newline returns true if the given rune is a Linux, Darwin, or Windows newline character

func Normalize

func Normalize(slice []rune) []rune

Normalize is a recursive function that will take all whitespace left of the left most non-whitespace and non-control-character space and move it somewhere in the interior starting on the left most interior and working in

Normalize maintains the []rune's width

Example ```go

s := []rune("    A string with whitespace to the left")
s = rslice.Normalize(s)

// s should now be "A  string  with  whitespace  to the left"

```

func NormalizeWhitespace

func NormalizeWhitespace(slice []rune) []rune

NormalizeWhitespace takes the left and right whitespace of the given rune slice and spreads it across the interior whitespace of the rune slice between the inner and outer most non-whitespace character

Maintains the []rune's width

Returns the slice unchanged if the []rune contains only whitespace, has no length, or has fewer than 2 words since there would be no inner whitespace to utilize

Usage: ```go

s := []rune("    A string with whitespace to the left and right    ")
s = rslice.NormalizeWhitespace(s)

// s should now be "A  string  with  whitespace  to  the   left and  right"

```

func ShiftLeft

func ShiftLeft(slice []rune) []rune

ShiftLeft shifts the rune slice one to the left and returns a copy if the slice is not all whitespace

func ShiftRight

func ShiftRight(slice []rune) []rune

ShiftRight shits the rune slice one to the right and returns a copy if the slice is not all whitespace

func ShiftWhitespaceLeft

func ShiftWhitespaceLeft(slice []rune) []rune

ShiftWhitespaceLeft shifts any whitespace right of the last non-whitespace character to the left of the first non-whitespace character in the rune slice and returns a copy if the slice is not all whitespace

func ShiftWhitespaceRight

func ShiftWhitespaceRight(slice []rune) []rune

ShiftWhitespaceRight shifts any whitespace left of the first non-whitespace character to the right of the last non-whitespace character in the rune sluce and returns a copy if the slice is not all whitespace

func TrimExcessWhitespace

func TrimExcessWhitespace(slice []rune) []rune

TrimExcessWhitespace will remove any occurance of whitespace greater than one count

func Valid

func Valid(slice []rune) bool

Valid returns true a slice has width and is not all whitespace

func Whitespace

func Whitespace(slice []rune) bool

Whitespace returns true if the entire []rune is whitespace It will also return true if the slice is empty

func Words

func Words(slice []rune) int

Words returns a count of non-whitespace groupings of characters that may or may not be a word

Can be useful to discover how many areas of whitespace you have for purposes such as full text justification across a width

Does not recognize control characters (such as \n or \t) as non-whitespace characters as per unicode stdlib

Types

This section is empty.

Jump to

Keyboard shortcuts

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