[Gambas-user] Text-Wrapping

Bruce adamnt42 at gmail.com
Wed Nov 11 14:25:39 CET 2020

On 11/11/20 11:00 pm, Hans Lehmann wrote:
> Hello,
> since there are true string artists among the readers, I just ask: I'm 
> looking for a function like LikeWrap(arg1Text, arg2Length) with which 
> you can wrap a text. At most arg2Length characters should be in one line.
> Line breaks in the original text should be preserved. A hyphenation is 
> not used. The text must be separated by a hyphen in the last word. The 
> length rule also applies in this case.
> Does anybody have a finished function in his source code box?

Let's clarify a few things before assumptions are made.
You want to break up a long string by inserting "\n" at the nearest 
right hand word boundary to position arg2Length. At face value that is 
reasonably fairly simple, but there are "twisty little passages".
A simple algorithm COULD be something like:
	While there are any charactes in arg1Text...
		set index to arg2Length
		while arg1Text[index] is not a space decrement index
		excise the substring [1..index] and add it to a working string array, 
say "wkLines".
	Join the wkLines into an output string with "\n"'s

Now here's the twisty bits.
a) Do you want the function to keep or remove the trailing space(s)?
b) What is to happen if the arg1Text contains a string of arg2Length spaces?
c) What is to happen if arg1Text (at any point) contains a "word" that 
is longer than arg2Length?
d) That algorithm is (deliberately) silent as to what happens with the 
final sub-string.
e) What is to happen if the arg1Text string is empty?
f) Fullstops (periods), commas, colons, semi-colons, question marks, 
exclamation marks - in fact any unpaired punctuation at the break 
position can be tricky.
g) We are talking about ASCII strings here aren't we?
h) Paired punctuation () [] {} '' "" <> can be tricky.
i) (advanced students only) Other delimiters like | \ and / can be 
tricky and lets not forget "doubled" delimiters like #! $$...$$ /*...*/ etc.
z) I presume that the output string would be sent to a fixed width 
target e.g. stdout as chopping a string this way to put in a textual 
control is near n useless.

bruce (the lower case one)

More information about the User mailing list