beetlebug▾
/

ztcl::text bounds

Gives the character index of each boundary of a string.

ztcl::text bounds ?-unit grapheme|word|sentence? STRING

Description

Gives the index of each boundary of a unit, from 0 to the length of STRING. The indexes count code points, as string index and string range do, so a unit runs from one index to the next index minus 1. An empty STRING gives 0.

Arguments

ArgumentTypeDefaultDescription
STRINGstringrequiredThe string to cut.

Options

OptionTypeDefaultDescription
-unitgrapheme, word, or sentencegraphemeGives the unit to cut at.

-unit UNIT

Gives the unit to cut at.

grapheme gives the boundaries of graphemes, word of words -all, and sentence of sentences. The value has no short form.

Values: grapheme, word, sentence.

% ztcl::text bounds -unit word "hi there"
0 2 3 8

% ztcl::text bounds -unit sentence "A b. C d."
0 5 9
% ztcl::text bounds -unit line a
bad unit "line": must be grapheme, word, or sentence
-errorcode TCL LOOKUP INDEX value

% ztcl::text bounds -unit w a
bad unit "w": must be grapheme, word, or sentence
-errorcode TCL LOOKUP INDEX value

Returns

A list of integers.

Examples

Each example of this page runs after package require ztcl::text.

% ztcl::text bounds "ab́c"
0 1 3 4

% ztcl::text bounds ""
0

Errors

% ztcl::text bounds
wrong # args: should be "ztcl::text bounds ?-unit grapheme|word|sentence? string"
-errorcode TCL WRONGARGS

% ztcl::text bounds -bogus a
wrong # args: should be "ztcl::text bounds ?-unit grapheme|word|sentence? string"
-errorcode TCL WRONGARGS

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1
Compute cost
One compute unit for each 4,096 bytes of the string.

See also