Friday, September 19, 2008

upper & lower case

Sometimes in writing QUEST logics, especially those that deal with user input or reading from text files, I've had to compare two pieces of text for equality where the case of the text wasn't an issue.  The thing with SCL is that text comparisons are case sensitive, which meant I needed a way to convert a text string to either all caps or all lower case characters.  I never found anything for this in QUEST's SCL docs, so I made my own, and here they are.  The routines work by reading each character in a string individually, and seeing if it's already in the desired case.  If not, we just add or subtract 32 to/from its ASCII value to convert between upper and lower case characters in ASCII text.


routine is_lower( the_string : String ) : Integer
Const
   first_lower 97
   last_lower 122
   upper_to_lower 32
Var
   the_char : String  
   result : Integer
Begin
   the_char = leftstr( the_string , 1 )
   if( asc( the_char ) <= last_lower AND asc( the_char ) >= first_lower ) then
      result = true
   else
      result = false
   endif
   return result
End

routine is_upper( the_string : String ) : Integer
Const
   first_upper 65
   last_upper 90
   upper_to_lower 32
Var
   the_char : String  
   result : Integer
Begin
   the_char = leftstr( the_string , 1 )
   if( asc( the_char ) <= last_upper AND asc( the_char ) >= first_upper ) then
      result = true
   else
      result = false
   endif
   return result
End          

routine upper( the_string : String ) : String  
--Return upper case version of the_string
Const
   first_lower 97
   last_lower 122
   upper_to_lower 32
Var
   i : Integer
   result , check_char : String  
Begin
result = ''
for i = 1 to len( the_string ) do
   check_char = substr( the_string , i , 1 )
   if( is_lower( check_char ) ) then
      check_char = chr( asc( check_char ) - 32 )
   endif
   result = result + check_char
endfor
return result
End

routine lower( the_string : String ) : String  
--Return upper case version of the_string
Const
   first_lower 97
   last_lower 122
   upper_to_lower 32
Var
   i : Integer
   result , check_char : String  
Begin
result = ''
for i = 1 to len( the_string ) do
   check_char = substr( the_string , i , 1 )
   if( is_upper( check_char ) ) then
      check_char = chr( asc( check_char ) + 32 )
   endif
   result = result + check_char
endfor
return result
End

No comments: