Introducing Tcl 8.7 Part 6: string operations

Published

This is the sixth in a series of posts about new features in the upcoming version 8.7 of Tcl. This post deals with operations on strings.

To take Tcl 8.7 for a spin, you can download a pre-alpha binary for your platform. Alternatively, you can build it yourself from the core-8-branch branch in the Tcl fossil repository.

New string comparison operators

Historically, the string compare and string equal commands were intended to be used to compare strings. However, the <, == etc. arithmetic operators could be used for comparison and being more succint, were often preferred by programmers. However these had the problem that they would do numeric, not string, comparison if both operands could be interpreted as numbers. This led to some strange behavior like the following example from TIP 461:

% expr {"0x10" < "0y"};# Compared as strings since 0y cannot be a number
1
% expr {"0y" < "1"};   # Compared as strings for the same reason
1
% expr {"1" < "0x10"}; # Compared as numerics as both can be interpreted as such
1

As you see, this mixed semantics breaks the normal assumption of transitivity of <. The eq and ne operators were introduced in Tcl 8.4 as explicit string comparison operators in lieu of == and != but string equivalents for <, <=, > and >= were never added. Tcl 8.7 fixes this by adding the lt, le, gt and ge string operators which will always treat both operands as strings even if they appear to be numeric. So for example,

% expr {"1" < "0x10"};  # Numeric compare
1
% expr {"1" lt "0x10"}; # String compare
0

Note that like eq and ne, these new operators are also commands in the ::tcl::mathop namespace.

% tcl::mathop::lt "1" "0x10"
0

The string insert command

The string insert command returns a copy of a string modified by inserting another string at a given index. It takes the form

string insert STRING INDEX INSERTSTRING

Prior to 8.7, generating a new string by inserting into a string was relatively cumbersome using some combination of string replace, string range, append, interpolation etc. Moreover, if the index was desired to be in one of the relative forms, it could get non-trivial. The string insert command now makes such operations straightforward.

As always, INDEX may be an integer, the keyword end signifying the position after the last character or a simple expression as illustrated in the following.

% string insert abd end-1 c
abcd

References

  1. TIP 461: Separate Numeric and String Comparison Operators

  2. TIP 504: New subcommand string insert

  3. mathop manpage

  4. string manpage