Deleting whitespace at start of string but...
Dave-ee Jones 13 Oct 2017
Hoi!
So, I've got my programming language parser working pretty mint but then realised that it's removing the whitespace on the end of a line. So if I've got a snippet that looks like this:
And I have a space write after 'hey!' on the same line (e.g. 'hey! ') then it will remove the space after 'hey!', which is what I don't want because I want 'what's up?' to appear a space after 'hey!', if you catch my drift.
Here's my current whitespace-removing-technique:
It removes the whitespace line-by-line.
Any help is appreciated. I'm assuming it's just an alteration to the current 'string:match(..)' I have, but since I'm not familiar with the character codes I cannot figure it out myself (maybe remove '(.-)%s*$' at the end?).
Edited by Dave-ee Jones, 13 October 2017 - 05:25 AM.
So, I've got my programming language parser working pretty mint but then realised that it's removing the whitespace on the end of a line. So if I've got a snippet that looks like this:
<color[red,white]> <write> hey! what's up? </write> </color>
And I have a space write after 'hey!' on the same line (e.g. 'hey! ') then it will remove the space after 'hey!', which is what I don't want because I want 'what's up?' to appear a space after 'hey!', if you catch my drift.
Here's my current whitespace-removing-technique:
_line:match("^%s*(.-)%s*$")
It removes the whitespace line-by-line.
Any help is appreciated. I'm assuming it's just an alteration to the current 'string:match(..)' I have, but since I'm not familiar with the character codes I cannot figure it out myself (maybe remove '(.-)%s*$' at the end?).
Edited by Dave-ee Jones, 13 October 2017 - 05:25 AM.
Kepler 13 Oct 2017
Here's what I use:
-- http://snippets.luac..._from_string_76
Edited by Kepler, 13 October 2017 - 05:53 AM.
-- http://snippets.luac..._from_string_76
function trim(s) return s:find'^%s*$' and '' or s:match'^%s*(.*%S)' end -- trim whitespace from left end of string function triml(s) return s:match'^%s*(.*)' end -- trim whitespace from right end of string function trimr(s) return s:find'^%s*$' and '' or s:match'^(.*%S)' end
Edited by Kepler, 13 October 2017 - 05:53 AM.
Grim Reaper 15 Oct 2017
If I understand what you're asking, here's a quick solution I thought up. My Lua skills are rusty, but the test scenario you described checks out.
Edited by Grim Reaper, 15 October 2017 - 05:33 AM.
local codeSnippet = [[ hey! what's up? ]] local function parseLineTrimWhiteSpaceAtBackKeepAtFront(line) return line:match("[%s]*([^%s]*%s*)") end local function parseSnippetIntoSingleLine(codeSnippet) local parsedLines = {} local lineNumber = 1 for line in codeSnippet:gmatch("[^\n+]+") do parsedLines[lineNumber] = line lineNumber = lineNumber + 1 end return parsedLines end local function combineParsedLinesIntoSingleLine(parsedLines) local combinedLine = "" for _, line in ipairs(parsedLines) do combinedLine = combinedLine .. line end return combinedLine end local function getContentLineFromCodeSnippet(codeSnippet) local parsedLines = parseSnippetIntoSingleLine(codeSnippet) return combineParsedLinesIntoSingleLine(parsedLines) end print(getContentLineFromCodeSnippet(codeSnippet)) -- Output (quotes to define string start/stop): "hey! what's up?"
Edited by Grim Reaper, 15 October 2017 - 05:33 AM.