strsplit

package
v1.147.1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package strsplit solves the common text-wrapping problem of splitting strings into bounded-size chunks without breaking Unicode characters and while trying to keep human-readable boundaries (spaces, punctuation, and newlines).

Problem

Naive string slicing by byte index is unsafe for UTF-8 text and can split a multi-byte rune in the middle, producing invalid output. Even when output stays valid, hard cuts in the middle of words make messages difficult to read (for example in chat payload limits, SMS segmentation, logs, or fixed-size transport frames).

strsplit provides chunking helpers that are UTF-8 aware and separator-aware, so chunks stay valid and readable.

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:

  1. Unicode whitespace.
  2. Unicode punctuation (kept with the preceding chunk).
  3. 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

This package is ideal for any Go application that needs robust, Unicode-aware message segmentation under byte-size constraints.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk

func Chunk(s string, size, n int) []string

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

func ChunkLine(s string, size, n int) []string

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.

Jump to

Keyboard shortcuts

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