1. word wrap?
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.
Thanks,,,,
Kenneth Rhodes
Lyons, GA
2. Re: word wrap?
>Does any know how to accomplish a word wrap in Euphoria?
Taken from a modified BUZZ.EX. This wordwrap is much better than the one
provided by the default BUZZ.EX. (Unless it's been modified from 1.5a)
constant WRAP=80 -- Wrap to 80 characters
sequence string
integer pos
pos = WRAP -- Start where the wrap is needed
while pos < length(string) do
if string[pos] = ' ' then -- Found a space?
string[pos] = '\n' -- Replace space with newline
pos = pos + WRAP -- Or pos+=WRAP to be cool if you
-- have 2.1
else
pos = pos - 1 -- No space, so go backwards.
end if
end while
puts(1, string)
I don't know how this responds to strings with words that are over WRAP in
length, but I suspect it may loop forever. This code also assumes there are
no newlines and stuff in the string. It should be easy enough to modify to
fix this two problems.
>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.
Just use the & operator:
-- Insert '\n' at place 50. (Does not replace)
string = string[1..50] & '\n' & string[51..length(string)]
3. Re: word wrap?
The wordwrap function in the attached include takes into account the
initial cursor position. If the input string is too long, it is
scanned to find the position of the last space character and a new
newline is inserted just past that point. If there are no spaces in
the preceding string segment, the newline character is simply inserted
at the right margin. Embedded newlines as well as carriage returns are
also considered.
I have done virtually no testing on this, so please be carefull. jiri
-- snip ------------------------------------------------------------
-- file : wordwrap.e
-- author: jiri babor
-- date : 99-01-28
-- email : jbabor at paradise.net.nz
-- topic : wordwrap function
integer right_margin
global procedure set_right_margin(integer i)
right_margin=i
end procedure
global function wordwrap(sequence text)
-- if input string is longer
integer b,c,i,max_length
sequence pos,s
pos=machine_func(25,0) -- get_position() - don't include graphic.e
max_length=right_margin-pos[2]+1
s=""
b=0 -- last blank position
i=1 -- search position in string
while length(text)>max_length do
c=text[i]
if i>max_length then
if not b then b=i-1 end if
s&=text[1..b]&'\n'
text=text[b+1..length(text)]
b=0
i=1
max_length=right_margin
elsif c='\n' or c='\r' then
s&=text[1..i]
text=text[i+1..length(text)]
b=0
i=1
max_length=right_margin
else
if c=' ' then b=i end if
i+=1
end if
end while
return s & text
end function
right_margin=80 -- default
4. Re: word wrap?
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.