|
1 | 1 | function Read-LuaMultiLineString { |
2 | 2 | <# |
3 | 3 | .SYNOPSIS |
4 | | - Reads a multi-line Lua string delimited by [[ and ]]. |
| 4 | + Reads a multi-line Lua string delimited by long brackets [[ ]], [=[ ]=], [==[ ]==], etc. |
5 | 5 | #> |
6 | 6 | [OutputType([string])] |
7 | 7 | [CmdletBinding()] |
|
10 | 10 | begin {} |
11 | 11 |
|
12 | 12 | process { |
13 | | - $script:luaPos += 2 # skip [[ |
| 13 | + # Count the number of '=' characters in the opening long bracket |
| 14 | + $script:luaPos++ # skip first [ |
| 15 | + $equalsCount = 0 |
| 16 | + while ($script:luaPos -lt $script:luaString.Length -and |
| 17 | + $script:luaString[$script:luaPos] -eq '=') { |
| 18 | + $equalsCount++ |
| 19 | + $script:luaPos++ |
| 20 | + } |
| 21 | + if ($script:luaPos -ge $script:luaString.Length -or |
| 22 | + $script:luaString[$script:luaPos] -ne '[') { |
| 23 | + throw 'Invalid long bracket string opening.' |
| 24 | + } |
| 25 | + $script:luaPos++ # skip second [ |
| 26 | + |
| 27 | + # Build the closing pattern: ] + N '=' + ] |
| 28 | + $closingBracket = ']' + ('=' * $equalsCount) + ']' |
| 29 | + $closeLen = $closingBracket.Length |
| 30 | + |
14 | 31 | $result = [System.Text.StringBuilder]::new() |
15 | 32 |
|
16 | | - # Per Lua spec, a newline immediately after [[ is ignored |
| 33 | + # Per Lua spec, a newline immediately after the opening bracket is ignored |
17 | 34 | if ($script:luaPos -lt $script:luaString.Length -and |
18 | 35 | $script:luaString[$script:luaPos] -eq "`n") { |
19 | 36 | $script:luaPos++ |
|
23 | 40 | $script:luaPos += 2 |
24 | 41 | } |
25 | 42 |
|
26 | | - while ($script:luaPos + 1 -lt $script:luaString.Length) { |
27 | | - if ($script:luaString[$script:luaPos] -eq ']' -and |
28 | | - $script:luaString[$script:luaPos + 1] -eq ']') { |
29 | | - $script:luaPos += 2 |
| 43 | + while ($script:luaPos -lt $script:luaString.Length) { |
| 44 | + if ($script:luaPos + $closeLen - 1 -lt $script:luaString.Length -and |
| 45 | + $script:luaString.Substring($script:luaPos, $closeLen) -eq $closingBracket) { |
| 46 | + $script:luaPos += $closeLen |
30 | 47 | return $result.ToString() |
31 | 48 | } |
32 | 49 | $null = $result.Append($script:luaString[$script:luaPos]) |
|
0 commit comments