stringutil

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package stringutil provides utilities for string manipulation, focusing on operations with runes and bytes for Unicode strings.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Capitalize added in v0.0.5

func Capitalize(words ...string) []string

func CountPrefix

func CountPrefix(s string, isPrefix func(rune) bool) int

CountPrefix returns the number of runes, starting from the 0th, that return true for isPrefix.

func EnsurePrefix added in v0.0.5

func EnsurePrefix(s, prefix string) string

func Indent

func Indent(text string) string

Indent a block of text using a "default" IndentOption:

EmptyLines:   EmptyLine_TrimHead | EmptyLine_TrimTail
IndentPrefix: ""
IndentString: "\t"

func Indentf

func Indentf(text string, args ...any) string

Indentf a block of text using a "default" IndentOption and format it:

EmptyLines:   EmptyLine_TrimHead | EmptyLine_TrimTail
IndentPrefix: ""
IndentString: "\t"

func Lowercase added in v0.0.5

func Lowercase(words ...string) []string

func TransCase added in v0.0.5

func TransCase(input string, from, to StringCase) string

Types

type IndentEmptyLineFlag

type IndentEmptyLineFlag uint

IndentEmptyLineFlag defines flags for handling empty lines during indentation.

const (
	// EmptyLine_TrimFirst trims the first line if it's empty.
	EmptyLine_TrimFirst IndentEmptyLineFlag = 1 << iota
	// EmptyLine_TrimHead trims all leading empty lines.
	EmptyLine_TrimHead
	// EmptyLine_TrimBody trims empty lines within the text body.
	EmptyLine_TrimBody
	// EmptyLine_TrimTail trims all trailing empty lines.
	EmptyLine_TrimTail
	// EmptyLine_TrimLast trims the last line if it's empty.
	EmptyLine_TrimLast

	// EmptyLine_TrimAll is a convenience flag to trim all empty lines.
	EmptyLine_TrimAll = EmptyLine_TrimHead | EmptyLine_TrimBody | EmptyLine_TrimTail
)

type IndentOption

type IndentOption struct {
	EmptyLines   IndentEmptyLineFlag // Flags for which empty lines to trim.
	IndentPrefix string              // Prefix for all non-empty lines.
	IndentString string              // The string used for indentation, e.g., "  " or "\t".
}

IndentOption specifies options for indenting a block of text. It includes settings for handling empty lines and defining indentation strings.

func (IndentOption) Indent

func (io IndentOption) Indent(text string) string

Indent applies the IndentOption to a given block of text, returning the indented text. It processes the text to indent non-empty lines according to the specified options and trims empty lines based on the EmptyLines flags.

type String

type String []rune

String represents a string as a slice of runes. String typifies a string through a rune slice, facilitating direct Unicode character manipulation.

Example
package main

import (
	"fmt"

	"github.com/toolvox/utilgo/pkg/stringutil"
)

func main() {
	// A complex string containing ASCII, Japanese Kanji and Hiragana, Greek characters, and emojis
	complexStr := stringutil.String([]rune("Go🚀は素晴らしい! Κόσμος👍"))

	// Display initial string
	fmt.Println("Original string:", complexStr.String())

	// Reverse the string rune-wise and print each character
	// Demonstrating manual rune manipulation
	reversedRunes := make([]rune, 0, complexStr.Len())
	for i := complexStr.Len() - 1; i >= 0; i-- {
		reversedRunes = append(reversedRunes, complexStr[i])
	}
	fmt.Println("Reversed by runes:", string(reversedRunes))

	// Extract "は素晴らしい!" using ByteSlice by calculating byte positions manually
	// Assuming known positions here for simplicity
	extract := complexStr.ByteSlice(6, 25) // Note: Adjust byte positions for your specific string
	fmt.Println("Extracted segment (by bytes):", extract.String())

	// Find and extract the Greek and emoji part, "Κόσμος👍", using rune positions
	// This assumes knowledge of the string structure
	greekPart := complexStr.Slice(-7, -1)
	fmt.Println("Greek part (by runes):", greekPart.String())

	// Demonstrating conversion back to bytes and manipulating bytes
	byteData := greekPart.Bytes()
	fmt.Printf("Greek part in bytes (hex): % x\n", byteData)

}
Output:

Original string: Go🚀は素晴らしい! Κόσμος👍
Reversed by runes: 👍ςομσόΚ !いしら晴素は🚀oG
Extracted segment (by bytes): は素晴らしい!
Greek part (by runes): Κόσμος👍
Greek part in bytes (hex): ce 9a cf 8c cf 83 ce bc ce bf cf 82 f0 9f 91 8d

func (String) Append

func (s String) Append(runes String) String

Append the runes of another String to this one.

func (String) ByteLen

func (s String) ByteLen() int

ByteLen returns the length of the String in bytes. This might differ from String.Len() for strings containing multi-byte characters.

Example
package main

import (
	"fmt"

	"github.com/toolvox/utilgo/pkg/stringutil"
)

func main() {
	str := stringutil.String([]rune("Hello, 世👍界👎"))
	fmt.Println(str.ByteLen())
}
Output:

21

func (String) ByteSlice

func (s String) ByteSlice(from, to int) String

ByteSlice returns a new String consisting of a subset of the original String's bytes, starting from 'from' and ending at 'to'. It handles out-of-range indices gracefully.

Example
package main

import (
	"fmt"

	"github.com/toolvox/utilgo/pkg/stringutil"
)

func main() {
	str := stringutil.String([]rune("Hello, 世👍界👎"))
	sliced := str.ByteSlice(0, 5)
	fmt.Println(sliced.String())
}
Output:

Hello

func (String) ByteSplice

func (s String) ByteSplice(from, to int, splice String) String

ByteSplice returns a new String consisting of a splicing of the `splice` String into original String's bytes, starting from 'from' and ending at 'to'. It handles out-of-range indices gracefully.

func (String) Bytes

func (s String) Bytes() []byte

Bytes converts the String into a slice of bytes. This is useful for operations or functions that require raw byte data.

Example
package main

import (
	"fmt"

	"github.com/toolvox/utilgo/pkg/stringutil"
)

func main() {
	str := stringutil.String([]rune("Hello, 世👍界👎"))
	fmt.Printf("% x\n", str.Bytes())
}
Output:

48 65 6c 6c 6f 2c 20 e4 b8 96 f0 9f 91 8d e7 95 8c f0 9f 91 8e

func (String) Clone

func (s String) Clone() String

Clone makes a deep copy of the String.

func (String) CountPrefix

func (s String) CountPrefix(isPrefix func(rune) bool) int

CountPrefix returns the number of runes, starting from the 0th, that return true for isPrefix.

func (String) Equals

func (s String) Equals(other String) bool

Equals returns true when both [String]s have the same runes.

func (String) Len

func (s String) Len() int

Len returns the number of runes in the String.

Example
package main

import (
	"fmt"

	"github.com/toolvox/utilgo/pkg/stringutil"
)

func main() {
	str := stringutil.String([]rune("Hello, 世👍界👎"))
	fmt.Println(str.Len())
}
Output:

11

func (String) Slice

func (s String) Slice(from, to int) String

Slice returns a new String consisting of a subset of the original String's runes, starting from 'from' and ending at 'to'. It handles out-of-range indices gracefully.

Example
package main

import (
	"fmt"

	"github.com/toolvox/utilgo/pkg/stringutil"
)

func main() {
	str := stringutil.String([]rune("Hello, 世👍界👎"))
	sliced := str.Slice(8, 10)
	fmt.Println(sliced.String())
}
Output:

👍界

func (String) Splice

func (s String) Splice(from, to int, splice String) String

Splice returns a new String consisting of a splicing of the `splice` String into original String's runes, starting from 'from' and ending at 'to'. It handles out-of-range indices gracefully.

func (String) String

func (s String) String() string

String converts the String (slice of runes) back into a standard Go string.

Example
package main

import (
	"fmt"

	"github.com/toolvox/utilgo/pkg/stringutil"
)

func main() {
	str := stringutil.String([]rune("Hello, 世👍界👎"))
	fmt.Println(str.String())
}
Output:

Hello, 世👍界👎

type StringCase added in v0.0.5

type StringCase int
const (
	// camelCaseText
	CamelCase StringCase = iota
	// PascalCaseText
	PascalCase
	// snake_case_text
	SnakeCase
	// Title Case Text
	TitleCase
)

func (StringCase) Parse added in v0.0.5

func (c StringCase) Parse(phrase string) []string

func (StringCase) Render added in v0.0.5

func (c StringCase) Render(words []string) string

Jump to

Keyboard shortcuts

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