{"modulePath":"github.com/wavetermdev/waveterm","version":"v0.14.5","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package utilfn\n\n## Constants\n\n```go\nconst NoStrPos = -1\n```\nsentinel value for StrWithPos.Pos to indicate no position\n\n## Variables\n\n```go\nvar ErrOverflow = errors.New(\"integer overflow\")\n```\n\n```go\nvar HexDigits = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}\n```\n\n```go\nvar PTLoc *time.Location\n```\n\n## Functions\n\n```go\nfunc AddElemToSliceUniq[T comparable](arr []T, elem T) []T\n```\n\n```go\nfunc AddInt(left, right int) (int, error)\n```\nAdd two int values, returning an error if the result overflows.\n\n```go\nfunc AddIntSlice(vals ...int) (int, error)\n```\nAdd a slice of ints, returning an error if the result overflows.\n\n```go\nfunc AppendNonZeroRandomBytes(b []byte, randLen int) []byte\n```\n\n```go\nfunc AtoiNoErr(str string) int\n```\n\n```go\nfunc AtomicRenameCopy(dstPath string, srcPath string, perms os.FileMode) error\n```\n\n```go\nfunc ByteMapsEqual(m1 map[string][]byte, m2 map[string][]byte) bool\n```\n\n```go\nfunc ChunkSlice[T any](s []T, chunkSize int) [][]T\n```\n\n```go\nfunc CombineMaps[V any](m1 map[string]V, m2 map[string]V)\n```\nwill overwrite m1 with m2's values\n\n```go\nfunc CombineStrArrays(sarr1 []string, sarr2 []string) []string\n```\ncombines string arrays and removes duplicates (returns a new array)\n\n```go\nfunc CompareAsFloat64(a, b any) bool\n```\nHelper to handle numeric comparisons as float64\n\n```go\nfunc CompareAsMarshaledJson(a, b any) bool\n```\n\n```go\nfunc ContainsBinaryData(data []byte) bool\n```\nContainsBinaryData checks if the provided data contains binary (non-text) content\n\n```go\nfunc ContainsStr(strs []string, test string) bool\n```\n\n```go\nfunc ConvertInt(val any) int64\n```\nconverts an int, int64, or float64 to an int64 nil or bad type returns 0\n\n```go\nfunc ConvertMap(val any) map[string]any\n```\n\n```go\nfunc ConvertToWallClockPT(t time.Time) time.Time\n```\n\n```go\nfunc ConvertUUIDv4Tov7(uuidv4 string) (string, error)\n```\n\n```go\nfunc CopyToChannel(outputCh chan\u003c- []byte, reader io.Reader) error\n```\ndoes \\*not\\* close outputCh on EOF or error\n\n```go\nfunc CopyWithEndBytes(outputBuf *bytes.Buffer, reader io.Reader, endBytes []byte) (bool, error)\n```\nreturns (isEOF, error)\n\n```go\nfunc DecodeDataURL(dataURL string) (mimeType string, data []byte, err error)\n```\nDecodeDataURL decodes a data URL and returns the mimetype and raw data bytes\n\n```go\nfunc DecodeStringArray(barr []byte) ([]string, error)\n```\n\n```go\nfunc DecodeStringMap(barr []byte) (map[string]string, error)\n```\n\n```go\nfunc DoMapStructure(out any, input any) error\n```\ndoes a mapstructure using \"json\" tags\n\n```go\nfunc DrainChannelSafe[T any](ch \u003c-chan T, debugName string)\n```\nDrainChannelSafe will drain a channel until it is empty or until a timeout is reached.\n\n```go\nfunc DumpGoRoutineStacks(w io.Writer)\n```\n\n```go\nfunc EllipsisStr(s string, maxLen int) string\n```\n\n```go\nfunc EncodeStringArray(arr []string) []byte\n```\n\n```go\nfunc EncodeStringMap(m map[string]string) []byte\n```\n\n```go\nfunc EncodedStringArrayGetFirstVal(encoded []byte) string\n```\non encoding error returns \"\" this is used to perform logic on first value without decoding the entire array\n\n```go\nfunc EncodedStringArrayHasFirstVal(encoded []byte, firstKey string) bool\n```\n\n```go\nfunc FilterValidArch(arch string) (string, error)\n```\n\\*\n\n  - Utility function to convert know architecture patterns\n  - to the patterns we use. It returns an error if the\n  - provided name is unknown\n\n```go\nfunc FindStringInSlice(slice []string, val string) int\n```\n\n```go\nfunc FormatLsTime(t time.Time) string\n```\n\n```go\nfunc FormatRelativeTime(modTime time.Time) string\n```\n\n```go\nfunc GetBool(v interface{}, field string) bool\n```\n\n```go\nfunc GetCmdExitCode(cmd *exec.Cmd, err error) int\n```\n\n```go\nfunc GetExitCode(err error) int\n```\n\n```go\nfunc GetFirstLine(s string) string\n```\n\n```go\nfunc GetJsonTag(field reflect.StructField) string\n```\n\n```go\nfunc GetLineColFromOffset(barr []byte, offset int) (int, int)\n```\n\n```go\nfunc GetMapKeys[K comparable, V any](m map[K]V) []K\n```\n\n```go\nfunc GetOrderedMapKeys[V any](m map[string]V) []string\n```\n\n```go\nfunc GetOrderedStringerMapKeys[K interface {\n\tcomparable\n\tfmt.Stringer\n}, V any](m map[K]V) []K\n```\n\n```go\nfunc GetStrArr(v interface{}, field string) []string\n```\n\n```go\nfunc GracefulClose(closer io.Closer, debugName, closerName string) bool\n```\n\n```go\nfunc HasBinaryData(data []byte) bool\n```\n\n```go\nfunc IndentString(indent string, str string) string\n```\n\n```go\nfunc IsBinaryContent(data []byte) bool\n```\n\n```go\nfunc IsNumericType(val any) bool\n```\nHelper to check if a value is a numeric type\n\n```go\nfunc IsPrefix(strs []string, test string) bool\n```\n\n```go\nfunc JsonMapToStruct(m map[string]any, v interface{}) error\n```\n\n```go\nfunc JsonValEqual(a, b any) bool\n```\nthis is a shallow equal, but with special handling for numeric types it will up convert to float64 and compare\n\n```go\nfunc LongestPrefix(root string, strs []string) string\n```\n\n```go\nfunc MapToStruct(in map[string]any, out any) error\n```\n\n```go\nfunc MarshalIndentNoHTMLString(v any, prefix, indent string) (string, error)\n```\nMarshalIndentNoHTMLString marshals the value to JSON with indentation and SetEscapeHTML(false), returning a string\n\n```go\nfunc MarshalJSONString(s string) string\n```\nMarshalJSONString marshals a string to JSON format, returning the properly escaped JSON string. Returns empty string if there's an error (rare).\n\n```go\nfunc MergeStrMaps[T any](m1 map[string]T, m2 map[string]T) map[string]T\n```\n\n```go\nfunc MoveSliceIdxToFront[T any](arr []T, idx int) []T\n```\n\n```go\nfunc MustPrettyPrintJSON(v any) string\n```\n\n```go\nfunc NullDecodeStr(barr []byte) (string, error)\n```\n\n```go\nfunc NullEncodeStr(s string) []byte\n```\nencodes a string, removing null/zero bytes (and separators '|') a zero byte is encoded as \"\\\\0\", a '\\\\' is encoded as \"\\\\\\\\\", sep is encoded as \"\\\\s\" allows for easy double splitting (first on \\\\x00, and next on \"|\")\n\n```go\nfunc ParsePartialJson(data []byte) (any, error)\n```\n\n```go\nfunc Ptr[T any](x T) *T\n```\n\\*\n\n  - Utility function for referencing a type with a pointer.\n  - This is the same as dereferencing with \u0026, but unlike \u0026\n  - you can directly use it on the ouput of a function\n  - without needing to create an intermediate variable\n\n```go\nfunc QuickHashString(s string) string\n```\n\n```go\nfunc QuickJson(v interface{}) string\n```\n\n```go\nfunc QuickParseJson[T any](s string) T\n```\n\n```go\nfunc RandomHexString(numHexDigits int) (string, error)\n```\nevery byte is 4-bits of randomness\n\n```go\nfunc ReUnmarshal(out any, in any) error\n```\n\n```go\nfunc ReadLineWithTimeout(ch chan LineOutput, timeout time.Duration) (string, error)\n```\n\n```go\nfunc RemoveElemFromSlice[T comparable](arr []T, elem T) []T\n```\nremoves an element from a slice and modifies the original slice (the backing elements) if it removes the last element from the slice, it will return nil so we free the original slice's backing memory\n\n```go\nfunc SafeDeref[T any](x *T) T\n```\n\\*\n\n  - Helper function that will deref a pointer if not null\n  - but returns a default value if it is null.\n\n```go\nfunc SendWithCtxCheck[T any](ctx context.Context, ch chan\u003c- T, val T) bool\n```\n\n```go\nfunc Sha1Hash(data []byte) string\n```\nreturns base64 hash of data\n\n```go\nfunc ShellHexEscape(s string) string\n```\nreturns hex escaped string (\\\\xNN for each byte)\n\n```go\nfunc ShellQuote(val string, forceQuote bool, maxLen int) string\n```\nminimum maxlen=6, pass -1 for no max length\n\n```go\nfunc SliceIdx[T comparable](arr []T, elem T) int\n```\n\n```go\nfunc SortStringRunes(s string) string\n```\n\n```go\nfunc StarMatchString(pattern string, s string, delimiter string) bool\n```\nmatches a delimited string with a pattern string the pattern string can contain \"\\*\" to match a single part, or \"\\*\\*\" to match the rest of the string note that \"\\*\\*\" may only appear at the end of the string\n\n```go\nfunc StrArrayToMap(sarr []string) map[string]bool\n```\n\n```go\nfunc StrMapsEqual(m1 map[string]string, m2 map[string]string) bool\n```\n\n```go\nfunc StrSetIntersection(s1 []string, s2 []string) []string\n```\n\n```go\nfunc StreamToLines(input io.Reader, lineFn func([]byte), readCallback func()) error\n```\n\n```go\nfunc StreamToLinesChan(input io.Reader) chan LineOutput\n```\nstarts a goroutine to drive the channel line output does not include the trailing newline\n\n```go\nfunc StrsEqual(s1arr []string, s2arr []string) bool\n```\n\n```go\nfunc StructToJsonMap(v interface{}) (map[string]any, error)\n```\n\n```go\nfunc StructToMap(in any) (map[string]any, error)\n```\n\n```go\nfunc TimeoutFromContext(ctx context.Context, defaultTimeout time.Duration) time.Duration\n```\n\n```go\nfunc ToFloat64(val any) (float64, bool)\n```\nConvert various numeric types to float64 for comparison\n\n```go\nfunc ToInt(val any) (int, bool)\n```\n\n```go\nfunc ToInt64(val any) (int64, bool)\n```\n\n```go\nfunc ToStr(val any) (string, bool)\n```\n\n```go\nfunc TruncateString(s string, maxLen int) string\n```\n\n```go\nfunc WriteFileIfDifferent(fileName string, contents []byte) (bool, error)\n```\n\n```go\nfunc WriteTemplateToFile(fileName string, templateText string, vars map[string]string) error\n```\n\n## Types\n\n```go\ntype LineOutput struct {\n\tLine  string\n\tError error\n}\n```\n\n```go\ntype LineWriter struct {\n\t// contains filtered or unexported fields\n}\n```\nLineWriter is an io.Writer that processes data line-by-line via a callback. Lines do not include the trailing newline. Lines longer than maxLineLength are dropped.\n\n```go\nfunc NewLineWriter(lineFn func([]byte)) *LineWriter\n```\nNewLineWriter creates a new LineWriter with the given callback function.\n\n```go\nfunc (lw *LineWriter) Flush()\n```\nFlush outputs any remaining buffered data as a final line. Should be called when the input stream is complete (e.g., at EOF).\n\n```go\nfunc (lw *LineWriter) Write(p []byte) (n int, err error)\n```\nWrite implements io.Writer, processing the data and calling the callback for each complete line.\n\n```go\ntype StrWithPos struct {\n\tStr string `json:\"str\"`\n\tPos int    `json:\"pos\"` // this is a 'rune' position (not a byte position)\n}\n```\n\n```go\nfunc ParseToSP(s string) StrWithPos\n```\n\n```go\nfunc (sp StrWithPos) Append(str string) StrWithPos\n```\n\n```go\nfunc (sp StrWithPos) Prepend(str string) StrWithPos\n```\n\n```go\nfunc (sp StrWithPos) String() string\n```\n\n","path":"github.com/wavetermdev/waveterm/pkg/util/utilfn","name":"utilfn","synopsis":"","isRedistributable":true}
