package com.penrillian.longstringloader;

import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LongStringUtils
{
	@NonNull
	public static List<String> getSplitString(String stringToSplit, int stringSplitLength)
	{
		/*
		length to split string needs to be at least as long as the longest series of non-whitespace in the string.
		E.G. if the following is the longest series of non-whitespace in the string
			licences:
		then stringSplitLength needs to be at least 9 (8 letters plus a colon)
		 */
		stringSplitLength = Math.max(getLengthOfLongestWord(stringToSplit), stringSplitLength);

		List<String> splitString = new ArrayList<>();

		Pattern p = Pattern.compile("\\G\\s*(.{1,"+ stringSplitLength +"})(?=\\s|$)", Pattern.DOTALL);
		Matcher m2 = p.matcher(stringToSplit);
		while (m2.find())
		{
			splitString.add(m2.group(1));
		}
		return splitString;
	}

	public static int getLengthOfLongestWord(String stringToSplit)
	{
		int longestWordLength = 0;
		Matcher m = Pattern.compile("\\s*(\\S+)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE).matcher(stringToSplit);
		while(m.find())
		{
			longestWordLength = Math.max(longestWordLength, m.group(1).length());
		}
		return  longestWordLength;
	}
}

@@ -334,16 +334,17 @@
 %0A%09%09/*%0A%09%09
+%09
 length t
@@ -444,16 +444,17 @@
 ring.%0A%09%09
+%09
 E.G. if 
@@ -524,16 +524,18 @@
 ring%0A%09%09%09
+%09%22
 licences
@@ -535,17 +535,19 @@
 icences:
-%0A
+%22%0A%09
 %09%09then s

Tidy up