Determining the class of a string

Although a string is generally considered to be an alphanumeric character, it can also belong to a class of strings. These classes allow us to manipulate the string in a manner consistent with its class type. For example, adding 1 to the character # will produce an error or unexpected return value. To assist us in determining the class of the string, Tcl provides the is keyword and a list of associated classes.

The syntax for the string command is as follows:

	string is class -strict -failindex variable string

When invoked with the is keyword the command will return 1, if the class is matched.

The classes is will check for are as follows:

Class

Description

alnum

Any Unicode alphabetic or digit character.

alpha

Any Unicode alphabetic character.

ascii

Any character with a value less than u0080 (those that are in the 7-bit ASCII range).

boolean

Any of the forms allowed by TCL_GetBoolean.

control

Any Unicode control character.

digit

Any Unicode digit character.

This includes characters outside of the [0-9] range.

double

Any of the valid forms for a double in Tcl, with optional surrounding whitespace.

In case of under/overflow in the value, 0 is returned and the varname will contain -1.

false

Any of the forms allowed to Tcl_GetBoolean where the value is false.

graph

Any Unicode printing character, except space.

integer

Any of the valid string formats for a 32-bit integer value in Tcl, with optional surrounding whitespace.

In case of under/overflow in the value, 0 is returned and the varname will contain -1.

list

Any proper list structure, with optional surrounding whitespace.

In case of improper list structure, 0 is returned and the varname will contain the index of the "element" where the list parsing fails or -1 if this cannot be determined.

lower

Any Unicode lower case alphabet character.

print

Any Unicode printing character, including space.

punct

Any Unicode punctuation character.

space

Any Unicode space character.

true

Any of the forms allowed to Tcl_GetBoolean where the value is true.

upper

Any upper case alphabet character in the Unicode character set.

wideinteger

Any of the valid forms for a wide integer in Tcl, with optional surrounding whitespace.

In case of under/overflow in the value, 0 is returned and the varname will contain -1.

wordchar

Any Unicode word character.

Any alphanumeric character, and any Unicode connector punctuation characters, for example an underscore.

xdigit

Any hexadecimal digit character ([0-9A-Fa-f]).

How to do it…

As you can see care has been taken to provide a full listing of the various classes of strings to cover all situations. In the following example we will determine if a string is a member of the digit class. Return values from the commands are provided for clarity. Enter the following command:


% string is digit a
0

How it works…

As you can see, string has returned a 0, informing us that the character supplied is not a member of the digit class. While this is a very simple implementation of this specific keyword, it is an invaluable tool to ensure that the class desired has been provided prior to utilization. Try using various classes and string values to see the various classes and how they react when given the correct or incorrect member of a class. When invoked with the is keyword, the string command returns 1 if the class being tested for is matched, otherwise it will return a 0. By applying the optional strict switch, an empty string will return 0 as opposed to 1. This is useful to determine the existence of an empty string. If the optional failindex switch is passed and 0 is the return value, the index where the string failed the class test will be stored in variable. If the return value is 1, the variable will not be set.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.104.27