Re: word wrap?
- Posted by Falkon1313 <falkon1313 at AOL.COM> Jan 28, 1999
- 562 views
On Wed, 27 Jan 1999 10:51:27 -0500, Kenneth Rhodes <krhodes at CYBERSOUTH.COM> wrote: >Hello, > >Several string related questions for anyone........ help would be >appreciated. > >Does any know how to accomplish a word wrap in Euphoria? > >Is it possible to insert a '\n' into a string? I'm thinking the answer >might be useing some of Norman Blais string functions,,,,,,, but I haven't >been able to see the light yet. Hello! I have done word-wrapping before, but in my code, I would break down the string into a set of strings, where each element is one line, stripping out all '\n's. Then wrap it. Then I made a function to rebuild a string from a setofstrings, inserting the desired spacer between lines. (a space or '\n' or whatever) -----------Source (and test code)-------- function StripNewLines( sequence Line ) --remove all '\n's from a text string, breaking the --string into a block of individual lines integer posInL sequence block posInL = 1 block = {} if ( length( Line ) < 2 ) then return( { Line } ) --avoid bug if line is only 1 character. end if for count = 1 to length( Line ) do if Line[count] = '\n' then block = append( block, Line[posInL..count-1] ) posInL = count + 1 end if end for if posInL < length( Line ) then block = append( block, Line[posInL..length( Line )] ) end if return( block ) end function function wrap_str( sequence Line, integer dLength ) --wraps a string of text to the desired length, --returning a sequence of strings <= that length --note: breaks at spaces, tabs, and newlines and strips out -- the characters at the break points atom oLL, count sequence sLine, wLines, tempL count = 1 sLine = StripNewLines( Line ) wLines = {} tempL = {} while 1 do if count <= length( sLine ) then tempL = tempL & sLine[count] count = count + 1 elsif length( tempL ) = 0 then exit end if oLL = length( tempL ) if oLL <= dLength then wLines = append( wLines, tempL ) tempL = {} else for subcount = dLength to 1 by -1 do if find( tempL[subcount], "\t " ) then wLines = append( wLines, tempL[1..subcount-1] ) tempL = tempL[subcount+1..oLL] exit end if end for if ( length( tempL ) = oLL ) then wLines = append( wLines, tempL[1..dLength] ) tempL = tempL[dLength + 1..oLL] end if end if end while return( wLines ) end function function concatblock( sequence s, sequence spacer ) --concatenate a block of text strings into a single string, --placing spacers between elements if desired sequence t t = {} if compare( s, {} ) then t = s[1] for c = 2 to length( s ) do t = t & spacer & s[c] end for end if return( t ) end function procedure showit( sequence s ) for c = 1 to length( s ) do puts( 1, s[c] & "\n" ) end for end procedure ------Test code sequence s, sb integer k procedure wrapnshow( integer l ) sb = wrap_str( s, l ) showit( sb ) puts( 1, "\n" ) k = getc( 0 ) end procedure s = "Twas brillig and the slithy toves "& "did gyre and gimble in the wabe, "& "all mimsy were the borogoves, "& "and the mome raths outgrabe." puts( 1, s & "\n" ) -- see what it looks like unwrapped k = getc( 0 ) -- try various wrap lengths... for l = 80 to 20 by -20 do wrapnshow( l ) end for --sb now equals s wrapped to a length of 20, let's rebuild with "\n"s sb = concatblock( sb, "\n" ) puts( 1, sb ) puts( 1, "\n^-----(Reconcatenated to one string.)\n\n" ) wrapnshow( 10 ) wrapnshow( 5 ) -- ^test to see what happens when wrap length is less than word length -- not pretty, but it works.