You can do it with a regular expression.
I have developed following regular expression and used it successfully with Java (I haven't tried it yet, but it should also work with Perl). The regular expression got a bit complicated, because I wanted it to work for any line length (not only 160) and for any text (also for empty text or short text or text with words that are longer than a line etc.), so I commented it well. The spaces should be preserved in a way that concatenationg all parts should give back the old string exactly as it was. If possible, the split should be done after the space.
If the string contains newline-characters, split it first by newline-character and then apply the regular expression on each part.
With help of the regular expression I will insert a newline-character everywhere where the split should occur, so I avoid passing arrays around (and concatenating array parts when saving to database or searching inside etc. later on).
In the explanation below, "4" is an example value and should be replaced with the value of the constant MAX_LINE_LENGTH.
The following regular expression splits the string into parts with length of maximum 4 characters using following rules in following order
- don't split if the whole line (or remainder) is less than 4 characters. regEx="(?s).{1,4}$"
- if the word is bigger than 4 characters, split the word inside. regEx="[^\s]{4}"
- split before last space if a space follows exactly after 4 characters. regEx="(?s).{4}(?=\s)"
- split behind last space before maximum 4 character regEx="(?s).{0,3}\s"
Example: splitting "12345 67 8 9ab c de wxyz1 2 4 5 678" yields "1234", "5 67", " 8 ", "9ab ", "c de", " ", "wxyz", "1 2 ", "4 5 ", "678".
In Java:
-
int maxLineLength=4; // usually you will get this value from your configuration file
-
String oldText="12345 67 8 9ab c de wxyz1 2 4 5 678"; // this is the String you want to split
-
-
// verify parameters.
-
// The line should be splitted at newline-characters already before.
-
if (maxLineLength < 1) throw new Exception("ERROR: maxLineLength is " + maxLineLength + ", but must be greater than 0!");
-
if (oldText == null) throw new Exception("ERROR: text must not be null!");
-
if (oldText.indexOf("\n") != -1) throw new Exception("ERROR: text must not contain newline characters!");
-
-
// quick way out to increase performance
-
if (oldText.isEmpty()) return "";
-
-
final String regularExpression = "(?s).{1," + maxLineLength + "}$|[^\\s]{" + maxLineLength + "}|(?s).{" + maxLineLength + "}(?=\\s)|(?s).{0," + (maxLineLength - 1) + "}\\s";
-
-
// insert newlines where we want to split the string into parts.
-
// Note: Trailing empty strings are not included in the resulting array of the split() method. So the newline-char at the end of newText will have no effect.
-
String newText = oldText.replaceAll(regularExpression, "$0\n"); // append newline
-
-
return newText;
-
Now you know the logic, so I leave it for you as an exercise to convert this program to Perl. Sorry, but I have no time any more to do it myself today. If you have difficulties, come back to me tomorrow and I will help you doing it.