Documentation
¶
Overview ¶
Package stringutils does things with atrings.
Some notes about this package:
- It does not use any logging.
- It does not use regular expressions anywhere.
- It accommodates XML string quoting conventions by allowing the use of either single quotes or double quotes.
- For functions that return a string or two plus a possibly-nil error, they can be wrapped in functions Must(..) or Must2(..), respectively, which either return successfully or a panic.
.
Index ¶
- Constants
- Variables
- func AddIndent(in string, indentString string, nr int) string
- func ColorDemo()
- func ConcatAll(values ...string) string
- func ConcatAllSpaced(values ...string) string
- func DeleteEmptyStrings(in []string) (out []string)
- func ElideCWD(s string) string
- func ElideHomeDir(s string) string
- func Enslice(in string) []string
- func FilterStringList(inlist, prefixes, midfixes, suffixes []string) []string
- func FilterStringsBySuffix(inputs []string, okayExts []string) (OKoutputs []string)
- func GetHomeDir() string
- func GetIndent(depth int) (pfx string)
- func IsInSliceIgnoreCase(s string, ss []string) bool
- func IsXmlQuote(s string) bool
- func IsXmlQuoted(txt string) bool
- func Ito09az(i int) string
- func LS_lh(fi fs.FileInfo, optPath string) string
- func MakeCSV(ss []string) string
- func MakeQuotedCSV(ss []string) string
- func MustXmlUnquote(txt string) string
- func NormalizeWhitespace(s string) string
- func Now() string
- func NowAsYM() string
- func NowAsYMDHM() string
- func NowPlus() string
- func PadLeftToLen(str, pad string, lingth int) string
- func PadRightToLen(str, pad string, lingth int) string
- func ParseYamlMetadata(instr string) (map[string]interface{}, error)
- func PrettifyISO(in string) string
- func SplitOffFirstWord(in string) (string, string)
- func SplitOffQuotedToken(in string) (string, string, error)
- func StringToBytes(s string) []byte
- func StripDelimiters(in string, delims string) (string, error)
- func StripQuotes(in string) (string, error)
- func Tildotted(s string) string
- func TrimMatchingDelims(txt string, delim string) (string, error)
- func TrimYamlMetadataDelimiters(s string) string
- func TruncateTo(in string, outmaxlen int) string
- func YamlMetadataHeaderRange(s string) (int, error)
- func Yn(b bool) string
- type PropSet
- type Raw_type
- type StringChainger
- type Stringser
- type Stringstack
Constants ¶
const PathSep = string(os.PathSeparator)
PathSep is a token nod to Windoze compatibility.
Variables ¶
var IndentationPrefix = " "
IndentationPrefix is for all sorts of output. TODO Make it configurable.
var Rbg, Ybg, Gbg, Rfg, Yfg, Gfg func(...interface{}) string
These are all functions that are created via the imported library color.
var Wfg, Blubg, Cyanbg func(...interface{}) string
Functions ¶
func AddIndent ¶
AddIndent prefixes `in` string with `nr` occurrences of `indentString`. Either string argument may be `""` i.e. empty. If `indentString` is the standard two (or whatever) spaces, use `GetIndent(..)` instead.
func ConcatAll ¶
ConcatAll concatenates all its (vargs) arguments into a new string. It uses a bytes.Buffer for efficiency.
func ConcatAllSpaced ¶
ConcatAllSpaced concatenates all its (vargs) arguments into a new string, with spaces inserted in-between 'em. It uses a bytes.Buffer for efficiency.
func DeleteEmptyStrings ¶
DeleteEmptyStrings returns the slice with any empty or all-whitespace strings deleted. It also compacts the slice, so element indices change.
func ElideCWD ¶
ElideCWD has effects that are session-specific, so it should only be used to make better user messages.
func ElideHomeDir ¶
ElideHomeDir doesn't try to deal with a relative path that starts with "."
func FilterStringList ¶
FilterStringList filters out filenames that match simple patterns (prefixes, "midfixes", suffixes). We provide this instead of some regex nonsense.
func FilterStringsBySuffix ¶
FilterStringsBySuffix takes a list of filenames and filters out those whose file extensions are not in the list that is passed in. NOTE:
- No periods on the okay file extensions.
- The comparison is case-INsensitive.
- This func is not currently used anywhere (2025.01), but it's useful so leave it in anyways.
func GetHomeDir ¶
func GetHomeDir() string
GetHomeDir is a convenience function, and refers to the invoking user's home directory.
func GetIndent ¶
GetIndent returns a string that has `depth` instances of the standard indent string (two spaces, unless otherwise modified).
func IsInSliceIgnoreCase ¶
IsInSliceIgnoreCase is like IsInSlice but without case matching.
func IsXmlQuote ¶
IsXmlQuote checks whether the string is either one single quote or one double quote.
func IsXmlQuoted ¶
IsXmlQuoted checks whether the string is surrounded by either single quotes or double quotes.
func Ito09az ¶
Ito09az converts its int arg (0..35) to a string of length one, in the range (for int 0..9) "0".."9", (for int 10..35) "a".."z"
func LS_lh ¶
LS_lh generates a file listing string like for "ls -l -h". If optPath is "" or "." then the file base name is taken from the FileInfo argument.
NOTE that this func is not currently used, but it's left here anyways for its potential usabililty. .
func MakeCSV ¶
MakeCSV returns the string slice as a CSV string. A final (comma + space) is omitted.
func MakeQuotedCSV ¶
MakeQuotedCSV places double-quotes around the output of MakeCSV.
func MustXmlUnquote ¶
MustXmlUnquote removes either paired single quotes or paired double quotes. It panics if neither is found.
func NormalizeWhitespace ¶
NormalizeWhitespace replaces weird whitespace junk (including newlines) with spaces.
func NowAsYMDHM ¶
func NowAsYMDHM() string
NowAsYMDHM maps (reversibly) the current time to "YMDhm" (where m is minutes / 2).
func NowPlus ¶
func NowPlus() string
NowPlus returns the current local date+time in a sensible format, i.e. "2006-01-02-mon/15:04:05/-07".
BTW "T" in ISO date-times is horrible for readability; use "_" instead.
func PadLeftToLen ¶
PadLeftToLen adds leading `pad` characters to hit the target length. Example: A new string 5 characters long, left-padded with spaces:
fmt.Println(PadLeftToLen("12", " ", 5)) // yields " 12"
func PadRightToLen ¶
PadRightToLen adds trailing `pad` characters to hit the target length.
Example: A new string 5 characters long, right-padded with zeros:
fmt.Println(PadRightToLen("12.", "0", 5)) // yields "12.00"
func ParseYamlMetadata ¶
ParseYamlMetadata tries to extract a YAML metadata block (YMB) - as a map - from the (start of the) input string `instring`.
Only simple fields are supported - no tree structure.
func SplitOffFirstWord ¶
SplitOffFirstWord splits the input string around the first whitespace, as defined by func strings.Fields, which uses unicode.IsSpace.
func SplitOffQuotedToken ¶
SplitOffQuotedToken expects the string to start with an XML quote (i.e. either a single quote or a double quote, but not any wrongly- named "smart quotes"); it splits off the entire quoted string, returned without the quotes, and the rest of the input string is returned in the other return value.
func StringToBytes ¶
StringToBytes uses both reflect and unsafe, so it's a bit of a turd.
func StripDelimiters ¶
StripDelimiters tries to remove corresponding characters from both ends of the input string. From the resulting string, surrounding spaces get trimmed. If an error is returned, the original string is also returned.
About the meaning of "corresponding":
- If `delimiters` is a single character (like a single or double quote), it tries to remove that character from both ends of the input string.
- If `delimiters` is two characters (like parentheses, braces, brackets), they are assumed to form a left/right pair, and so the ends of the input string are treated differently.
.
func StripQuotes ¶
StripQuotes tries to strip off matching XML quotes (i.e. either single or double quotes). From the resulting string, surrounding spaces get trimmed. If an error is returned, the original string is also returned.
func Tildotted ¶
Tildotted shortens a filepath by ediding the current user's home directory, or the current working directory, by eliding "~" or "." respectively.
func TrimMatchingDelims ¶
TrimMatchingDelims tried to strip off matching first and last characters. It is basically a simplified version of `StripDelimiters(..)`.
func TrimYamlMetadataDelimiters ¶
TrimYamlMetadataDelimiters supplies a trailing newline.
func TruncateTo ¶
TruncateTo truncates & ends the string with triple dots if it's too long.
func YamlMetadataHeaderRange ¶
YamlMetadataHeaderRange wants "---" at the start of the file at the start of a line (maybe after whitespace) to open the block, and "---" or "..." at the start of a new line to end the block. NOTE tho that the leading whitespace should already be trimmed away.
Types ¶
type PropSet ¶
func GetYamlMetadataAsPropSet ¶
GetYamlMetadataAsPropSet is a convenience function. It assume that all the metadata values are top-level and can be represented as strings. The metadata is unmarshalled into a map (i.e. a `PropSet`), so variables can be freely added, but there is no checking for required fields.
func YamlMapAsPropSet ¶
func YamlMapAsPropSet(u map[interface{}]interface{}) (ps PropSet)
YamlMapAsPropSet returns a PropSet, i.e. a map[string]string
type Raw_type ¶
type Raw_type D.SemanticFieldType
const ( // Raw_type_BIN is opaque Raw_type_BIN Raw_type = Raw_type(D.SFT_BLOB_) // Raw_type_XML is assumed to be well-formed Raw_type_XML Raw_type = Raw_type(D.SFT_XTEXT) // Raw_type_HTML is assumed to be HTML5 Raw_type_HTML Raw_type = Raw_type(D.SFT_HTEXT) // Raw_type_MKDN is assumed to be CommonMark (or GFM?) Raw_type_MKDN Raw_type = Raw_type(D.SFT_MTEXT) // Raw_type_SQL is, well, hey why not eh Raw_type_SQL Raw_type = Raw_type(D.SFT_QTEXT) // Raw_type_NIL is none (or too little) to figure out what kind Raw_type_NIL Raw_type = Raw_type(D.SFT_0TEXT) // Raw_type_DIRLIKE is a hack placeholder for consistent // handling (because of consistent problems in code): // IsDirlike is a more general case of IsDir() - // shorthand for "is not possible to have own content" - // but this can be more formally defined as "is/has link(s) // to other stuff" - i.e. it is a directory or symbolic link. // Used by [ctoken.TypedRaw]. Raw_type_DIRLIKE Raw_type = "dirlk" )
type StringChainger ¶
StringChainger is a func that is String In, String out, and therefore chainable.
NOTE this func is not currently used, but we keep it for possible future use.
type Stringser ¶
Stringser is a handy interface for content. Echo reproduces the content, Info is a normal level of debuggery, and Debug should be verbose.
type Stringstack ¶
type Stringstack []string
Stringstack is a LIFO stack for strings.
func (Stringstack) Peek ¶
func (ss Stringstack) Peek() string
Peek returns empty string ("") on an empty stack.
func (*Stringstack) Pop ¶
func (ss *Stringstack) Pop() string
Pop returns empty string ("") on an empty stack.
Source Files
¶
- colors.go
- concat.go
- doc.go
- elisions.go
- filter.go
- filterstringlist.go
- findinslice.go
- ls-lh.go
- makecsv.go
- maps.go
- now.go
- paddings.go
- rawtypes.go
- slicemisc.go
- splitoff.go
- stringchainger.go
- stringser.go
- stringstack.go
- stringtobytes.go
- stripdelimiters.go
- time.go
- trimmatching.go
- xmlquotes.go
- yamlmetadata.go
- yntruth.go