Re: Standard Euphoria Library Project
- Posted by David Cuny <dcuny at LANSET.COM> Feb 07, 2001
- 516 views
Here are some standard string functions I use, borrowing heavily from QBasic. Most are pretty self-explanatory, except for eof/get_string. They work much like QBasic's line input statement, and wrap gets() so you don't have to worry about it returning a -1 instead of a sequence. For example: integer handle sequence data data = {} -- open a file handle = open( "file.txt", "r" ) -- read until end of file while not eof( handle ) do -- get data from the file data &= { get_string( handle ) } end while -- close the file close( handle ) [The Routines] eof( integer file ) returns true if at end of file used with get_string() get_string( integer file ) returns string from file akin to qbasic's "line input" statement if end of file, returns empty sequence "safe" routine left( sequence s, integer len ) returns leftmost characters from string negative length removes those many chars from right side "safe" routine that does boundary checking ltrim( sequence s ) removes white space from left side of string lower_case( sequence s ) converts a string, or sequence of strings, to lower case mid( sequence s, integer start, integer len ) returns requested substring from string "safe" routine that does boundary checking right( sequence s, integer len ) returns rightmost characters from string negative length removes those many chars from left side "safe" routine that does boundary checking rtrim( sequence s ) removes white space from right side of string split( sequence s ) splits string into words upper_case( sequence s ) converts a string, or sequence of strings, to upper case -- David Cuny [The Code] -- string.e -- qbasic-style string support routines constant WhiteSpace = " \t\n\r" constant CaseDiff = 'a' - 'A' sequence lineRead lineRead = repeat( 0, 16 ) global function split( sequence s ) -- chop line into white-space delimited substrings object char sequence string, strings -- pad the end s &= ' ' -- clear accumulator strings = {} string = "" -- scan the string for i = 1 to length( s ) do char = s[i] -- not a char? if sequence( char ) then -- error, need to handle -- seperator? elsif find( char, WhiteSpace ) then -- word accumulated? if length( string ) then -- add to substrings list strings &= {string} string = "" end if else -- add to substring string &= char end if end for return strings end function global function ltrim( sequence s ) -- remove white space from left hand of string -- not empty? if length( s ) then for i = 1 to length( s ) do -- not white space? if not find( s[i], WhiteSpace ) then -- return string return s[i..length(s)] end if end for end if -- all whitespace, or empty return "" end function global function rtrim( sequence s ) -- remove white space from right hand of string -- not empty? if length( s ) then for i = length( s ) to 1 by -1 do -- not white space? if not find( s[i], WhiteSpace ) then -- return string return s[1..i] end if end for end if -- all whitespace, or empty return "" end function global function mid( sequence s, integer start, integer len ) -- emulate qbasic's mid$ function if start < 1 or len < 1 or start > length(s) then return "" elsif start+len-1 > length(s) then return s[start..length(s)] else return s[start..start+len-1] end if end function global function right( sequence s, integer len ) -- return the rightmost characters in the string. -- if len is negative, returns the string less leftmost -- len chars integer l -- get the length of the string l = length( s ) if len = 0 then -- empty string return "" elsif len > 0 then -- normal right$ if len > l then -- too large, return subset return s else -- return requested amount return s[l-len+1..l] end if else -- too large? if -len > l then -- return entire string return s else -- return substring return s[-len+1..l] end if end if end function global function left( sequence s, integer len ) -- return the leftmost characters in the string. -- if len is negative, returns the string less rightmost -- len chars integer l -- get the length of the string l = length( s ) if len = 0 then -- empty string return "" elsif len > 0 then -- normal right$ if len > l then -- too large, return subset return s else -- return requested amount return s[1..len] end if else -- too large? if -len > l then -- return entire string return s else -- return substring return s[1..l+len] end if end if end function global function eof( integer file ) -- no line in buffer? if equal( lineRead[file], 0 ) then -- read a line from the file lineRead[file] = gets( file ) end if -- return true if eof return integer( lineRead[file] ) end function global function get_string( integer file ) object result -- buffer empty? if equal( lineRead[file], 0 ) then -- read a line from the file result = gets( file ) else -- get stored value result = lineRead[file] -- store 0 in buffer lineRead[file] = 0 end if -- end of file? if integer( result ) then -- return empty string return "" end if -- remove line feed? if equal( right(result,1), "\n" ) then result = left(result,-1) end if -- return line read return result end function global function upper_case( sequence s ) integer char for i = 1 to length( s ) do if sequence( s[i] ) then s[i] = upper_case( s[i] ) else char = s[i] if char >= 'a' and char <= 'z' then s[i] = char - CaseDiff end if end if end for return s end function global function lower_case( sequence s ) integer char for i = 1 to length( s ) do if sequence( s[i] ) then s[i] = upper_case( s[i] ) else char = s[i] if char >= 'A' and char <= 'Z' then s[i] = char + CaseDiff end if end if end for return s end function