| 1 | // Package cat provides efficient and flexible string concatenation utilities. |
| 2 | // It includes optimized functions for concatenating various types, builders for fluent chaining, |
| 3 | // and configuration options for defaults, pooling, and unsafe optimizations. |
| 4 | // The package aims to minimize allocations and improve performance in string building scenarios. |
| 5 | package catscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/ |
| 6 | |
| 7 | import ( |
| 8 | "syncscip-go gomod github.com/golang/go/src go1.21 sync/" |
| 9 | "sync/atomicscip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/" |
| 10 | ) |
| 11 | |
| 12 | // Constants used throughout the package for separators, defaults, and configuration. |
| 13 | // These include common string literals for separators, empty strings, and special representations, |
| 14 | // as well as limits like recursion depth. Defining them as constants allows for compile-time |
| 15 | // optimizations, readability, and consistent usage in functions like Space, Path, CSV, and reflection handlers. |
| 16 | // cat.go (updated constants section) |
| 17 | const ( |
| 18 | emptyscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/empty. = "" // Empty string constant, used for checks and defaults. |
| 19 | spacescip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/space. = " " // Single space, default separator. |
| 20 | slashscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/slash. = "/" // Forward slash, for paths. |
| 21 | dotscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/dot. = "." // Period, for extensions or decimals. |
| 22 | commascip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/comma. = "," // Comma, for CSV or lists. |
| 23 | equalscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/equal. = "=" // Equals, for comparisons. |
| 24 | newlinescip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/newline. = "\n" // Newline, for multi-line strings. |
| 25 | |
| 26 | // SQL-specific constants |
| 27 | andscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/and. = "AND" // AND operator, for SQL conditions. |
| 28 | inOpenscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/inOpen. = " IN (" // Opening for SQL IN clause |
| 29 | inClosescip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/inClose. = ")" // Closing for SQL IN clause |
| 30 | asSQLscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/asSQL. = " AS " // SQL AS for aliasing |
| 31 | countscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/count. = "COUNT(" // SQL COUNT function prefix |
| 32 | sumscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/sum. = "SUM(" // SQL SUM function prefix |
| 33 | avgscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/avg. = "AVG(" // SQL AVG function prefix |
| 34 | maxOpenscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/maxOpen. = "MAX(" // SQL MAX function prefix |
| 35 | minOpenscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/minOpen. = "MIN(" // SQL MIN function prefix |
| 36 | caseSQLscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/caseSQL. = "CASE " // SQL CASE keyword |
| 37 | whenscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/when. = "WHEN " // SQL WHEN clause |
| 38 | thenscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/then. = " THEN " // SQL THEN clause |
| 39 | elseSQLscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/elseSQL. = " ELSE " // SQL ELSE clause |
| 40 | endscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/end. = " END" // SQL END for CASE |
| 41 | countAllscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/countAll. = "COUNT(*)" // SQL COUNT(*) for all rows |
| 42 | parenOpenscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/parenOpen. = "(" // Opening parenthesis |
| 43 | parenClosescip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/parenClose. = ")" // Closing parenthesis |
| 44 | |
| 45 | maxRecursionDepthscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/maxRecursionDepth. = 32 // Maximum recursion depth for nested structure handling. |
| 46 | nilStringscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/nilString. = "<nil>" // String representation for nil values. |
| 47 | unexportedStringscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/unexportedString. = "<?>" // Placeholder for unexported fields. |
| 48 | ) |
| 49 | |
| 50 | // Numeric is a generic constraint interface for numeric types. |
| 51 | // It includes all signed/unsigned integers and floats. |
| 52 | // Used in generic functions like Number and NumberWith to constrain to numbers. |
| 53 | type Numericscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/Numeric# interface { |
| 54 | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 |
| 55 | } |
| 56 | |
| 57 | // poolEnabled controls whether New() reuses Builder instances from a pool. |
| 58 | // Atomic.Bool for thread-safe toggle. |
| 59 | // When true, Builders from New must be Released to avoid leaks. |
| 60 | var poolEnabledscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/poolEnabled. atomicscip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/.Boolscip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/Bool# |
| 61 | |
| 62 | // builderPool stores reusable *Builder to reduce GC pressure on hot paths. |
| 63 | // Uses sync.Pool for efficient allocation/reuse. |
| 64 | // New func creates a fresh &Builder when pool is empty. |
| 65 | var builderPoolscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/builderPool. = syncscip-go gomod github.com/golang/go/src go1.21 sync/.Poolscip-go gomod github.com/golang/go/src go1.21 sync/Pool#{ |
| 66 | Newscip-go gomod github.com/golang/go/src go1.21 sync/Pool#New.: func() any { return &Builderscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/Builder#{} }, |
| 67 | } |
| 68 | |
| 69 | // Pool enables or disables Builder pooling for New()/Release(). |
| 70 | // When enabled, you MUST call b.Release() after b.String() to return it. |
| 71 | // Thread-safe via atomic.Store. |
| 72 | // Enable for high-throughput scenarios to reduce allocations. |
| 73 | func Poolscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/Pool().(enable bool) { poolEnabledscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/poolEnabled..Storescip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/Bool#Store().(enable) } |
| 74 | |
| 75 | // unsafeBytesFlag controls zero-copy []byte -> string behavior via atomics. |
| 76 | // Int32 used for atomic operations: 1 = enabled, 0 = disabled. |
| 77 | // Affects bytesToString function for zero-copy conversions using unsafe. |
| 78 | var unsafeBytesFlagscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/unsafeBytesFlag. atomicscip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/.Int32scip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/Int32# // 1 = true, 0 = false |
| 79 | |
| 80 | // SetUnsafeBytes toggles zero-copy []byte -> string conversions globally. |
| 81 | // When enabled, bytesToString uses unsafe.String for zero-allocation conversion. |
| 82 | // Thread-safe via atomic.Store. |
| 83 | // Use with caution: assumes the byte slice is not modified after conversion. |
| 84 | // Compatible with Go 1.20+; fallback to string(bts) if disabled. |
| 85 | func SetUnsafeBytesscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/SetUnsafeBytes().(enable bool) { |
| 86 | if enable { |
| 87 | unsafeBytesFlagscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/unsafeBytesFlag..Storescip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/Int32#Store().(1) |
| 88 | } else { |
| 89 | unsafeBytesFlagscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/unsafeBytesFlag..Storescip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/Int32#Store().(0) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // IsUnsafeBytes reports whether zero-copy []byte -> string is enabled. |
| 94 | // Thread-safe via atomic.Load. |
| 95 | // Returns true if flag is 1, false otherwise. |
| 96 | // Useful for checking current configuration. |
| 97 | func IsUnsafeBytesscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/IsUnsafeBytes().() bool { return unsafeBytesFlagscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/unsafeBytesFlag..Loadscip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/Int32#Load().() == 1 } |
| 98 | |
| 99 | // deterministicMaps controls whether map keys are sorted for deterministic output in string conversions. |
| 100 | // It uses atomic.Bool for thread-safe access. |
| 101 | var deterministicMapsscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/deterministicMaps. atomicscip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/.Boolscip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/Bool# |
| 102 | |
| 103 | // SetDeterministicMaps controls whether map keys are sorted for deterministic output |
| 104 | // in reflection-based handling (e.g., in writeReflect for maps). |
| 105 | // When enabled, keys are sorted using a string-based comparison for consistent string representations. |
| 106 | // Thread-safe via atomic.Store. |
| 107 | // Useful for reproducible outputs in testing or logging. |
| 108 | func SetDeterministicMapsscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/SetDeterministicMaps().(enable bool) { |
| 109 | deterministicMapsscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/deterministicMaps..Storescip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/Bool#Store().(enable) |
| 110 | } |
| 111 | |
| 112 | // IsDeterministicMaps returns current map sorting setting. |
| 113 | // Thread-safe via atomic.Load. |
| 114 | // Returns true if deterministic sorting is enabled, false otherwise. |
| 115 | func IsDeterministicMapsscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/IsDeterministicMaps().() bool { |
| 116 | return deterministicMapsscip-go gomod github.com/olekukonko/cat . `github.com/olekukonko/cat`/deterministicMaps..Loadscip-go gomod github.com/golang/go/src go1.21 `sync/atomic`/Bool#Load().() |
| 117 | } |