Documentation
¶
Overview ¶
Package strsplit splits strings into bounded-size chunks without breaking Unicode characters, keeping human-readable boundaries (spaces, punctuation, and newlines).
How It Works ¶
The package exposes two functions:
- Chunk: splits a full text block, prioritizing newline boundaries first, trimming whitespace per line, then delegating long lines to ChunkLine.
- ChunkLine: splits a single line by maximum byte size, ensuring the split point is at a rune boundary and preferring the closest separator before the limit.
Separator preference order in ChunkLine:
- Unicode whitespace.
- Unicode punctuation (kept with the preceding chunk).
- Hard UTF-8-safe cut when no separator exists.
Sizing Semantics ¶
size is a maximum byte length, not a rune count. Produced chunks never exceed size bytes, with one unavoidable exception: a single rune wider than size is emitted whole (splitting it would produce invalid UTF-8), so that one chunk may exceed size.
Line And Whitespace Handling ¶
Chunk treats only the newline byte "\n" as a line boundary. A carriage return "\r" is not a boundary on its own; it is removed only when it is adjacent to a "\n" (or otherwise leading/trailing), because each line is whitespace-trimmed. Other Unicode line separators (U+2028, U+2029, U+0085, ...) are likewise not treated as boundaries and may remain inside a chunk. Empty and whitespace-only lines are dropped, so blank lines between paragraphs never yield empty chunks and are not preserved.
Chunk Limit ¶
Both functions support an optional chunk limit n:
- n > 0: return at most n chunks.
- n < 0: unlimited chunks.
- n == 0: return nil.
Return Values ¶
Both functions return nil only for invalid arguments (size < 1 or n == 0). Otherwise they return a non-nil slice that may be empty (for example when the input is empty or contains only whitespace). Produced chunks are always whitespace-trimmed and never empty.
Usage ¶
chunks := strsplit.Chunk(text, 280, -1) // split full text block lineParts := strsplit.ChunkLine(line, 64, 3) // at most 3 chunks
When To Use ¶
- Text must fit a field or message size limit measured in bytes.
- Splitting must not cut through a multi-byte UTF-8 character or an emoji.
- Chunks should break at spaces, punctuation, or newlines where possible.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶
Chunk splits a text block into whitespace-trimmed substrings of at most size bytes, breaking on newline ("\n") boundaries first and then on separators within each long line. Empty and whitespace-only lines are dropped. It returns at most n chunks (n > 0), an unlimited number (n < 0), or nil (n == 0 or size < 1). See ChunkLine for the per-line separator rules and the wider-than-size rune caveat.
Example ¶
package main
import (
"fmt"
"github.com/tecnickcom/nurago/pkg/strsplit"
)
func main() {
str := "helloworld\nbellaciao"
d := strsplit.Chunk(str, 5, 3)
fmt.Println(d)
}
Output: [hello world bella]
func ChunkLine ¶
ChunkLine splits a single line into whitespace-trimmed substrings of at most size bytes, always cutting on a UTF-8 rune boundary and preferring the closest Unicode whitespace (then punctuation, kept with the preceding chunk) before the limit. A single rune wider than size is emitted whole, so that chunk may exceed size. It returns at most n chunks (n > 0), an unlimited number (n < 0), or nil (n == 0 or size < 1); produced chunks are never empty.
Example ¶
package main
import (
"fmt"
"github.com/tecnickcom/nurago/pkg/strsplit"
)
func main() {
str := "hello,world"
d := strsplit.ChunkLine(str, 8, -1)
fmt.Println(d)
}
Output: [hello, world]
Types ¶
This section is empty.