Re: quick 4lg question
Posted in 1998
Thanks Carlos, Looks like we missed the completely obvious here. Your function is clearer, shorter and faster, and in the spirit of open source software I'm going to absorb your suggestion. ---------------------------------------------------------------------- John H. Frantz Power-4gl: Extending Informix-4gl john@rl.is http://www.rl.is/~john/pow4gl.html Carlos Costa e Silva wrote: > > John H. Frantz wrote in message <357DA803.B42E5932@rl.is>... > >Dylan Tansey wrote: > >> > >> Is there an Informix function to remove leading spaces. I am looking for > >> the opposite of "clipped". > >> I searched all manuals but can't find anything useful. > > > >With compliments from the Power-4gl function library: > > > >function left(text) > > > >define > > text,ctxt char(256), > > i,j,k smallint > >let ctxt = " " > >let k = length(text) > >let j = null > >for i = 1 to k > > if j is null then > > if text[i,i] <> " " then let j = 1 > > else continue for > > end if > > end if > > let ctxt[j,j] = text[i,i] > > let j = j + 1 > >end for > >return ctxt clipped > > > > >end function > > Who wrote that function ? > > I think this is somewhat clearer and faster: > > function trim( str ) > define str char( 500 ) > define i,n smallint > > if str = ' ' or str is null then > return str > end if > let i = 1 > let n = length(str) > while str[ i ] = ' ' > let i = i + 1 > end while > return str[ i, n ] > end function > > This function is also useful: > > # remove all spaces from a string > function remove_spaces( wstr ) > define wstr char( 500 ) > define wstrx char( 500 ) > define i, j, n smallint > > if wstr = ' ' or wstr is null then > return str > end if > let j = 0 > let n = length( wstr ) > for i = 1 to n > if wstr[ i ] <> ' ' then > let j = j + 1 > let wstrx[ j ] = wstr[ i ] > end if > end for > return wstrx[ 1, j ] > end function > > With compliments from the Minimal-4gl function library > > > > >---------------------------------------------------------------------- > >John H. Frantz Power-4gl: Extending Informix-4gl > >john@rl.is http://www.rl.is/~john/pow4gl.html > > Ciao > > Carlos. > > ---- > Carlos Costa e Silva <ccs@minimal.pt> > Minimal Lda > Lisboa > Portugal > > PS. > > If anyone is interested here are some functions from our string library: > > # String.4gl - auxiliary string functions > # > # developed by Minimal Lda - Lisbon, Portugal > # > > define wordlist char(255) # used by ord() for performance reasons > > # returns TRUE if c (one character) exists in wstr > # else returns FALSE > function instr1( c, wstr ) > define c char( 1 ) > define wstr char( 512 ) > > define i, n smallint > > let n = length( wstr ) > for i = 1 to n > if wstr[ i ] = c then > return TRUE > end if > end for > return FALSE > end function > > # returns the first position of c in wstr > # 0 = not found > function instrc( c, wstr ) > define c char( 1 ) > define wstr char( 512 ) > define i, n smallint > > let n = length( wstr ) > for i = 1 to n > if wstr[ i ] = c then > return i > end if > end for > return 0 > end function > > # returns the first position of c in wstr > # counting from position wpos (included) > # 0 = not found > function instrcn( wpos, c, wstr ) > define wpos smallint > define c char( 1 ) > define wstr char( 512 ) > define i, n smallint > > let n = length( wstr ) > for i = wpos to n > if wstr[ i ] = c then > return i > end if > end for > return 0 > end function > > # returns the first position of the string win in wstring > # counting from wpos (included) > # 0 = not found > function instr( wpos, win, wstring ) > define wpos smallint > define win char( 20 ) > define wstring char( 2048 ) > > define len_in, len_in1, len_str, i, n smallint > > let len_in = length( win ) > let len_in1 = len_in - 1 > let len_str = length( wstring ) > > if len_in = 0 then > return 0 > end if > > let n = len_str - len_in1 > for i = wpos to n > if win[ 1, len_in ] = wstring[ i, i + len_in1 ] then > return i > end if > end for > return 0 > end function > > # returns the nth position of the string win in wstr > # counting from position wpos (included) > # 0 = not found > function instring( wpos, win, wstring ) > define wpos smallint > define win char( 20 ) > define wstring char( 512 ) > > define len_in, len_in1, len_str, i, n smallint > > let len_in = length( win ) > let len_in1 = len_in - 1 > let len_str = length( wstring ) > > if len_in = 0 then > return 0 > end if > > let n = len_str - len_in1 > for i = 1 to n > if win[ 1, len_in ] = wstring[ i, i + len_in1 ] then > let wpos = wpos - 1 > if wpos = 0 then > return i > end if > end if > end for > return 0 > end function # in_string() > > # breaks wstr in words > # if n > 0 returns all text following the first space before n > # if n < 0 returns all text up to the the first space before abs(n) > # > # in other words returns all text from the beginning of the word in position > n > # or all up to the word in position n > function break_text( wstr, n ) > define wstr char( 512 ) > define i, n, m smallint > > if n > 0 then > let m = n > else > let m = -n > end if > for i = m to 1 step -1 > if wstr[ i ] = ' ' then > exit for > end if > end for > > if n > 0 then > let m = length( wstr ) > if n >= m then > return ' ' > end if > if i <= 1 then > return wstr[ n + 1, m ] > end if > return wstr[ i + 1, m ] > end if > > if i <= 1 then > return wstr[ 1, m ] > end if > return wstr[ 1, i - 1 ] > > end function # partir() > > # break a string in words > # equal to break_text with this modification: > # if wstop exists in wstr, it breaks the text in wstop > # always does left trim of the returned string. > # > # called with break_text1( '.', -12, 'abcd. efg hijkl') > # returns 'abcd.' > function break_text1( wstr, n, wstop ) > define wstr char( 512 ) > define wstop char( 20 ) > define