4.8 The HLA Standard Library CHARS.HHF Module


4.8 The HLA Standard Library CHARS.HHF Module

The HLA Standard Library chars.hhf module provides a couple of routines that convert characters from one form to another and several routines that classify characters according to their graphic representation. These functions are especially useful for processing user input to verify that it is correct.

The first two routines we will consider are the translation/conversion functions. These functions are chars.toUpper and chars.toLower. These functions use the following syntax:

      chars.toLower( characterValue ); // Returns converted character in AL.      chars.toUpper( characterValue ); // Returns converted character in AL. 

These two functions require a byte-sized parameter (typically an 8-bit register or a char variable). They check the character to see if it is an alphabetic character; if it is not, then these functions return the unmodified parameter value in the AL register. If the character is an alphabetic character, then these functions may translate the value depending on the particular function. The chars.toUpper function translates lower case alphabetic characters to upper case; it returns other characters unmodified. The chars.toLower function does the converse: It translates upper case characters to lower case characters and leaves other characters alone.

These two functions are especially useful when processing user input containing alphabetic characters. For example, suppose you expect a "Y" or "N" answer from the user at some point in your program. You code might look like the following:

      forever           stdout.put( "Answer 'Y' or 'N':" );           stdin.FlushInput(); // Force input of new line of text.           stdin.getc(); // Read user input in AL.           breakif( al = 'Y' || al = 'N' );           stdout.put( "Illegal input, please reenter", nl );      endfor; 

The problem with this program is that the user must answer exactly "Y" or "N" (using upper case) or the program will reject the user's input. This means that the program will reject "y" and "n" because the ASCII codes for these characters are different than "Y" and "N."

One way to solve this problem is to include an additional breakif statement in the code above that test for "y" and "n" as well as "Y" and "N." The problem with this approach is that AL will still contain one of four different characters, complicating tests of AL once the program exits the loop. A better solution is to use either chars.toUpper or chars.toLower to translate all alphabetic characters to a single case. Then you can test AL for a single pair of characters, both in the loop and outside the loop. The resulting code would look like the following:

      forever           stdout.put( "Answer 'Y' or 'N':" );           stdin.FlushInput();      // Force input of new line of text.           stdin.getc();            // Read user input in AL.           chars.toUpper( al );     // Convert "y" and "n" to "Y" and "N".           breakif( al = 'Y' || al = 'N' );           stdout.put( "Illegal input, please reenter", nl );      endfor; << test for "Y" or "N" down here to determine user input >> 

As you can see from this example, the case conversion functions can be quite useful when processing user input. As a final example, consider a program that presents a menu of options to the user, and the user selects an option using an alphabetic character. Once again, you can use chars.toUpper or chars.toLower to map the input character to a single case so that it is easier to process the user's input:

      stdout.put( "Enter selection (A-G):" );      stdin.FlushInput();      stdin.getc();      chars.toLower( al );      if( al = 'a' ) then           << Handle Menu Option A >>      elseif( al = 'b' ) then           << Handle Menu Option B >>      elseif( al = 'c' ) then           << Handle Menu Option C >>      elseif( al = 'd' ) then           << Handle Menu Option D >>      elseif( al = 'e' ) then           << Handle Menu Option E >>      elseif( al = 'f' ) then           << Handle Menu Option F >>      elseif( al = 'g' ) then           << Handle Menu Option G >>      else           stdout.put( "Illegal input!" nl );      endif; 

The remaining functions in the chars.hhf module all return a boolean result depending on the type of the character you pass them as a parameter. These classification functions let you quickly and easily test a character to determine whether its type is valid for the some intended use. These functions expect a single-byte (char) parameter and they return true (1) or false (0) in the EAX register. These functions use the following calling syntax:

      chars.isAlpha( c );    // Returns true if c is alphabetic      chars.isUpper( c );    // Returns true if c is upper case alphabetic.      chars.isLower( c );    // Returns true if c is lower case alphabetic.      chars.isAlphaNum( c ); // Returns true if c is alphabetic or numeric.      chars.isDigit( c );    // Returns true if c is a decimal digit.      chars.isXDigit( c );   // Returns true if c is a hexadecimal digit.      chars.isGraphic( c );  // See notes below.      chars.isSpace( c );    // Returns true if c is a whitespace character.      chars.isASCII( c );    // Returns true if c is in the range #$00..#$7f.      chars.isCtrl( c );     // Returns true if c is a control character. 

Notes: Graphic characters are the printable characters whose ASCII codes fall in the range $21..$7E. Note that a space is not considered a graphic character (nor are the control characters). Whitespace characters are the space, the tab, the carriage return, and the line feed. Control characters are those characters whose ASCII code is in the range $00..$1F and $7F.

Although the chars.hhf module's classification functions handle many common situations, you may find that you need to test a character to see whether it belongs in a class that the chars.hhf module does not handle. Fear not, checking for such characters is very easy. The section on character sets later in this chapter will explain how to do this.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net