--Functions relating to Strings


-- take a string and break by spaces to an array
split_w = function (astring)   -- String sentence
    if (astring == nil) then
		print("split_w nil")
		return {}
	end

	local words = {}
	for w in string.gmatch(astring, "%S+") do
		table.insert(words,w)
	end
	return words	--array of the words
end
split_s = function(astring)
	local sentences = {}
	local sofar = ""
	local words = split_w(astring)
	if #words < 2 then return(words) end

	for  i,s in ipairs(words) do
		if (sofar == "") then		--no leading blank
			sofar = tostring(s)
		else
			sofar = sofar .." " ..  tostring(s)
		end


		if (i == #words) then
			--	 	add last word unconditionally and return
			table.insert(sentences,sofar)
			return sentences
		end

		if string.find(s,"%.") then
			table.insert(sentences,sofar)
			sofar = ""
		end
	end
	print("Should never get here")
end
test = "In Veteran White-Gold Tower, become completely engulfed in flame by the Planar Inhibitor's Heat Stroke attack before it completes its Daedric Catastrophe attack. Then stay alive until the Planar Inhibitor is defeated."
--split = split_w(test)
--print("returned a table of " .. #split .. "  words.")


flow_s = function( words_t, max_length )
--[[ given an array of words  { [1]="TENTENTEN0", [2] = "FOUR", [3]="THREE"}
	return an array of array of words so that the length is not greater than max_length
with max_length 10
  {  [1] = {[1]="TENTENTEN0"},  --array of 1 value
      [2] = {[1]="FOUR", [2]="THREE"}  --array of 2 value
	}
--]]
    if words_t == nil then
		print("words_t is nil")
		return nil
	end

	if type(max_length) ~= "number" then
		print("max_length is not a number")
		return nil
	end



	result = {}
	line_t = {}
	line_len = 0
	for i, word in ipairs (words_t) do
	--	print (word)
		if (string.len(word) + line_len) > max_length then
			table.insert(result,line_t)
			line_t = {}
			line_len = 0
		end
		table.insert(line_t, word)
		if line_len >  0 then
			line_len = line_len + string.len(word) +1		--Allow for Spaces
		else
			line_len = line_len + string.len(word)
		end
	end
	table.insert(result,line_t)
	return result
end


	join_s = function (words_tt)
--given an array of array of words (output from flow_s) Produce a string with \n
    if words_t == nil then
		print("parameter nil in join_s")
		return nil
	end

	local result = ""
	for _,words_t in ipairs (words_tt) do

		for _,word in ipairs (words_t) do


		end

	end


end

print(join_w(split_w(test)))