-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
234 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,93 @@ | ||
using sdmap.Functional; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
|
||
namespace sdmap.Utils | ||
{ | ||
public class StringUtil | ||
{ | ||
public static Result<string> Parse(string input) | ||
{ | ||
if (string.IsNullOrWhiteSpace(input)) | ||
return Result.Fail<string>($"string literial must not be empty."); | ||
|
||
if (input.Length <= 2) | ||
return Result.Fail<string>($"string literial length must > 2."); | ||
|
||
if (input.First() != input.Last()) | ||
return Result.Fail<string>($"string literial's first and last char must be the same."); | ||
|
||
var firstChar = input[0]; | ||
var escapedChar = Details.GetEscapedChar(firstChar); | ||
if (escapedChar.IsFailure) | ||
return Result.Fail<string>(escapedChar.Error); | ||
|
||
var inner = input.Substring(1, input.Length - 2); | ||
return Result.Ok(Details.EscapeNoCheck(inner, escapedChar.Value)); | ||
} | ||
|
||
public class Details | ||
{ | ||
public static Result<char> GetEscapedChar(char ch) | ||
{ | ||
if (ch == '\'') | ||
{ | ||
return Result.Ok('"'); | ||
} | ||
else if (ch == '"') | ||
{ | ||
return Result.Ok('\''); | ||
} | ||
else | ||
{ | ||
return Result.Fail<char>($"char {ch} not escappable."); | ||
} | ||
} | ||
|
||
public static string EscapeNoCheck(string input, char escapeChar) | ||
{ | ||
var unicodeRegex = new Regex(@"\\[uU]([0-9A-Fa-f]{4})"); | ||
|
||
var result = input | ||
.Replace(@"\\", @"\") | ||
.Replace(@"\/", @"/") | ||
.Replace(@"\t", "\t") | ||
.Replace(@"\b", "\b") | ||
.Replace(@"\f", "\f") | ||
.Replace(@"\n", "\n") | ||
.Replace(@"\r", "\r") | ||
.Replace(@"\t", "\t") | ||
.Replace($@"\{escapeChar}", escapeChar.ToString()); | ||
|
||
return unicodeRegex.Replace(result, match => | ||
((char)int.Parse(match.Value.Substring(2), NumberStyles.HexNumber)).ToString()); | ||
} | ||
} | ||
} | ||
} | ||
using sdmap.Functional; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
|
||
namespace sdmap.Utils | ||
{ | ||
public class StringUtil | ||
{ | ||
public static Result<string> Parse(string input) | ||
{ | ||
if (string.IsNullOrWhiteSpace(input)) | ||
return Result.Fail<string>($"string literial must not be empty."); | ||
|
||
if (input[0] == '@') | ||
{ | ||
return AtStringParse(input); | ||
} | ||
else | ||
{ | ||
return RegularStringParse(input); | ||
} | ||
} | ||
|
||
private static Result<string> RegularStringParse(string input) | ||
{ | ||
if (input.Length < 2) | ||
return Result.Fail<string>($"string literial length must > 2."); | ||
|
||
if (input.First() != input.Last()) | ||
return Result.Fail<string>($"string literial's first and last char must be the same."); | ||
|
||
var firstChar = input[0]; | ||
var escapedChar = Details.GetEscapedChar(firstChar); | ||
if (escapedChar.IsFailure) | ||
return Result.Fail<string>(escapedChar.Error); | ||
|
||
var inner = input.Substring(1, input.Length - 2); | ||
return Result.Ok(Details.EscapeNoCheck(inner, escapedChar.Value)); | ||
} | ||
|
||
private static Result<string> AtStringParse(string input) | ||
{ | ||
if (input.Length < 3) | ||
return Result.Fail<string>($"AtString literial length must > 3."); | ||
|
||
var inner = input.Substring(2, input.Length - 3); | ||
var escaped = inner.Replace("\"\"", "\""); | ||
return Result.Ok(escaped); | ||
} | ||
|
||
public class Details | ||
{ | ||
public static Result<char> GetEscapedChar(char ch) | ||
{ | ||
if (ch == '\'') | ||
{ | ||
return Result.Ok('"'); | ||
} | ||
else if (ch == '"') | ||
{ | ||
return Result.Ok('\''); | ||
} | ||
else | ||
{ | ||
return Result.Fail<char>($"char {ch} not escappable."); | ||
} | ||
} | ||
|
||
public static string EscapeNoCheck(string input, char escapeChar) | ||
{ | ||
var unicodeRegex = new Regex(@"\\[uU]([0-9A-Fa-f]{4})"); | ||
|
||
var result = input | ||
.Replace(@"\\", @"\") | ||
.Replace(@"\/", @"/") | ||
.Replace(@"\t", "\t") | ||
.Replace(@"\b", "\b") | ||
.Replace(@"\f", "\f") | ||
.Replace(@"\n", "\n") | ||
.Replace(@"\r", "\r") | ||
.Replace(@"\t", "\t") | ||
.Replace($@"\{escapeChar}", escapeChar.ToString()); | ||
|
||
return unicodeRegex.Replace(result, match => | ||
((char)int.Parse(match.Value.Substring(2), NumberStyles.HexNumber)).ToString()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
using static sdmap.Parser.G4.SdmapLexer; | ||
|
||
namespace sdmap.test.LexerTest | ||
{ | ||
public class StringTest : LexerTestBase | ||
{ | ||
[Fact] | ||
public void String1() | ||
{ | ||
var tokens = GetAllTokens("\"Test\""); | ||
Assert.Equal(1, tokens.Count); | ||
Assert.Equal(STRING, tokens[0].Type); | ||
} | ||
|
||
[Fact] | ||
public void String2() | ||
{ | ||
var tokens = GetAllTokens("'Test'"); | ||
Assert.Equal(1, tokens.Count); | ||
Assert.Equal(STRING, tokens[0].Type); | ||
} | ||
|
||
[Fact] | ||
public void EmptyStringIsOk() | ||
{ | ||
var tokens = GetAllTokens("''"); | ||
Assert.Equal(1, tokens.Count); | ||
Assert.Equal(STRING, tokens[0].Type); | ||
} | ||
|
||
[Fact] | ||
public void AtString() | ||
{ | ||
var tokens = GetAllTokens(@"@""Hel\/lo"""); | ||
Assert.Equal(1, tokens.Count); | ||
Assert.Equal(STRING, tokens[0].Type); | ||
Assert.Equal(@"@""Hel\/lo""", tokens[0].Text); | ||
} | ||
} | ||
} |
Oops, something went wrong.