diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractChar.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractChar.cs new file mode 100644 index 00000000..1c90fd9c --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractChar.cs @@ -0,0 +1,56 @@ +using System; +using System.Text; + +public static partial class Extensions +{ + /// A StringBuilder extension method that extracts the character described by @this. + /// The @this to act on. + /// The extracted character. + public static char ExtractChar(this StringBuilder @this) + { + return @this.ExtractChar(0); + } + + /// A StringBuilder extension method that extracts the character described by @this. + /// The @this to act on. + /// [out] The end index. + /// The extracted character. + public static char ExtractChar(this StringBuilder @this, out int endIndex) + { + return @this.ExtractChar(0, out endIndex); + } + + /// A StringBuilder extension method that extracts the character described by @this. + /// The @this to act on. + /// The start index. + /// The extracted character. + public static char ExtractChar(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractChar(startIndex, out endIndex); + } + + /// A StringBuilder extension method that extracts the character described by @this. + /// Thrown when an exception error condition occurs. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted character. + public static char ExtractChar(this StringBuilder @this, int startIndex, out int endIndex) + { + if (@this.Length > startIndex + 1) + { + var ch1 = @this[startIndex]; + var ch2 = @this[startIndex + 1]; + var ch3 = @this[startIndex + 2]; + + if (ch1 == '\'' && ch3 == '\'') + { + endIndex = startIndex + 2; + return ch2; + } + } + + throw new Exception("Invalid char at position: " + startIndex); + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractComment.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractComment.cs new file mode 100644 index 00000000..5f6eafb9 --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractComment.cs @@ -0,0 +1,64 @@ +using System.Text; + +public static partial class Extensions +{ + /// A StringBuilder extension method that extracts the comment described by @this. + /// The @this to act on. + /// The extracted comment. + public static StringBuilder ExtractComment(this StringBuilder @this) + { + return @this.ExtractComment(0); + } + + /// A StringBuilder extension method that extracts the comment described by @this. + /// The @this to act on. + /// [out] The end index. + /// The extracted comment. + public static StringBuilder ExtractComment(this StringBuilder @this, out int endIndex) + { + return @this.ExtractComment(0, out endIndex); + } + + /// A StringBuilder extension method that extracts the comment described by @this. + /// The @this to act on. + /// The start index. + /// The extracted comment. + public static StringBuilder ExtractComment(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractComment(startIndex, out endIndex); + } + + /// A StringBuilder extension method that extracts the comment described by @this. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted comment. + public static StringBuilder ExtractComment(this StringBuilder @this, int startIndex, out int endIndex) + { + if (@this.Length > startIndex + 1) + { + var ch1 = @this[startIndex]; + var ch2 = @this[startIndex + 1]; + + if (ch1 == '/' && ch2 == '/') + { + // Single line comment + + return @this.ExtractCommentSingleLine(startIndex, out endIndex); + } + + if (ch1 == '/' && ch2 == '*') + { + /* + * Multi-line comment + */ + + return @this.ExtractCommentMultiLine(startIndex, out endIndex); + } + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractCommentMultiLine.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractCommentMultiLine.cs new file mode 100644 index 00000000..b6a787f1 --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractCommentMultiLine.cs @@ -0,0 +1,92 @@ +using System.Text; + +public static partial class Extensions +{ + /// + /// A StringBuilder extension method that extracts the comment multi line described by + /// @this. + /// + /// The @this to act on. + /// The extracted comment multi line. + public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this) + { + return @this.ExtractCommentMultiLine(0); + } + + /// + /// A StringBuilder extension method that extracts the comment multi line described by + /// @this. + /// + /// The @this to act on. + /// [out] The end index. + /// The extracted comment multi line. + public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this, out int endIndex) + { + return @this.ExtractCommentMultiLine(0, out endIndex); + } + + /// + /// A StringBuilder extension method that extracts the comment multi line described by + /// @this. + /// + /// The @this to act on. + /// The start index. + /// The extracted comment multi line. + public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractCommentMultiLine(startIndex, out endIndex); + } + + /// + /// A StringBuilder extension method that extracts the comment multi line described by + /// @this. + /// + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted comment multi line. + public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this, int startIndex, out int endIndex) + { + var sb = new StringBuilder(); + + if (@this.Length > startIndex + 1) + { + var ch1 = @this[startIndex]; + var ch2 = @this[startIndex + 1]; + + if (ch1 == '/' && ch2 == '*') + { + /* + * Multi-line comment + */ + + sb.Append(ch1); + sb.Append(ch2); + var pos = startIndex + 2; + + while (pos < @this.Length) + { + var ch = @this[pos]; + pos++; + + if (ch == '*' && pos < @this.Length && @this[pos] == '/') + { + sb.Append(ch); + sb.Append(@this[pos]); + endIndex = pos; + return sb; + } + + sb.Append(ch); + } + + endIndex = pos; + return sb; + } + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractCommentSingleLine.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractCommentSingleLine.cs new file mode 100644 index 00000000..5ed2c153 --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractCommentSingleLine.cs @@ -0,0 +1,88 @@ +using System.Text; + +public static partial class Extensions +{ + /// + /// A StringBuilder extension method that extracts the comment single line described by + /// @this. + /// + /// The @this to act on. + /// The extracted comment single line. + public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this) + { + return @this.ExtractCommentSingleLine(0); + } + + /// + /// A StringBuilder extension method that extracts the comment single line described by + /// @this. + /// + /// The @this to act on. + /// [out] The end index. + /// The extracted comment single line. + public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this, out int endIndex) + { + return @this.ExtractCommentSingleLine(0, out endIndex); + } + + /// + /// A StringBuilder extension method that extracts the comment single line described by + /// @this. + /// + /// The @this to act on. + /// The start index. + /// The extracted comment single line. + public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractCommentSingleLine(startIndex, out endIndex); + } + + /// + /// A StringBuilder extension method that extracts the comment single line described by + /// @this. + /// + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted comment single line. + public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this, int startIndex, out int endIndex) + { + var sb = new StringBuilder(); + + if (@this.Length > startIndex + 1) + { + var ch1 = @this[startIndex]; + var ch2 = @this[startIndex + 1]; + + if (ch1 == '/' && ch2 == '/') + { + // Single line comment + + sb.Append(ch1); + sb.Append(ch2); + var pos = startIndex + 2; + + while (pos < @this.Length) + { + var ch = @this[pos]; + pos++; + + if (ch == '\r' && pos < @this.Length && @this[pos] == '\n') + { + endIndex = pos - 1; + return sb; + } + + sb.Append(ch); + } + + endIndex = pos; + return sb; + } + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractHexadecimal.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractHexadecimal.cs new file mode 100644 index 00000000..5785ffd9 --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractHexadecimal.cs @@ -0,0 +1,90 @@ +using System.Text; + +public static partial class Extensions +{ + /// A StringBuilder extension method that extracts the hexadecimal described by @this. + /// The @this to act on. + /// The extracted hexadecimal. + public static StringBuilder ExtractHexadecimal(this StringBuilder @this) + { + return @this.ExtractHexadecimal(0); + } + + /// A StringBuilder extension method that extracts the hexadecimal described by @this. + /// The @this to act on. + /// [out] The end index. + /// The extracted hexadecimal. + public static StringBuilder ExtractHexadecimal(this StringBuilder @this, out int endIndex) + { + return @this.ExtractHexadecimal(0, out endIndex); + } + + /// A StringBuilder extension method that extracts the hexadecimal described by @this. + /// The @this to act on. + /// The start index. + /// The extracted hexadecimal. + public static StringBuilder ExtractHexadecimal(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractHexadecimal(startIndex, out endIndex); + } + + /// A StringBuilder extension method that extracts the hexadecimal described by @this. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted hexadecimal. + public static StringBuilder ExtractHexadecimal(this StringBuilder @this, int startIndex, out int endIndex) + { + // WARNING: This method support all kind of suffix for .NET Runtime Compiler + // An operator can be any sequence of supported operator character + + if (startIndex + 1 < @this.Length && @this[startIndex] == '0' + && (@this[startIndex + 1] == 'x' || @this[startIndex + 1] == 'X')) + { + var sb = new StringBuilder(); + + var hasNumber = false; + var hasSuffix = false; + + sb.Append(@this[startIndex]); + sb.Append(@this[startIndex + 1]); + + var pos = startIndex + 2; + + while (pos < @this.Length) + { + var ch = @this[pos]; + pos++; + + if (((ch >= '0' && ch <= '9') + || (ch >= 'a' && ch <= 'f') + || (ch >= 'A' && ch <= 'F')) + && !hasSuffix) + { + hasNumber = true; + sb.Append(ch); + } + else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) + { + hasSuffix = true; + sb.Append(ch); + } + else + { + pos -= 2; + break; + } + } + + if (hasNumber) + { + endIndex = pos; + return sb; + } + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractKeyword.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractKeyword.cs new file mode 100644 index 00000000..da5a5f12 --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractKeyword.cs @@ -0,0 +1,80 @@ +using System.Text; + +public static partial class Extensions +{ + /// A StringBuilder extension method that extracts the keyword described by @this. + /// The @this to act on. + /// The extracted keyword. + public static StringBuilder ExtractKeyword(this StringBuilder @this) + { + return @this.ExtractKeyword(0); + } + + /// A StringBuilder extension method that extracts the keyword described by @this. + /// The @this to act on. + /// [out] The end index. + /// The extracted keyword. + public static StringBuilder ExtractKeyword(this StringBuilder @this, out int endIndex) + { + return @this.ExtractKeyword(0, out endIndex); + } + + /// A StringBuilder extension method that extracts the keyword described by @this. + /// The @this to act on. + /// The start index. + /// The extracted keyword. + public static StringBuilder ExtractKeyword(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractKeyword(startIndex, out endIndex); + } + + /// A StringBuilder extension method that extracts the keyword described by @this. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted keyword. + public static StringBuilder ExtractKeyword(this StringBuilder @this, int startIndex, out int endIndex) + { + // WARNING: This method support custom operator for .NET Runtime Compiler + // An operator can be any sequence of supported operator character + var sb = new StringBuilder(); + + var pos = startIndex; + var hasCharacter = false; + + while (pos < @this.Length) + { + var ch = @this[pos]; + pos++; + + if ((ch >= 'a' && ch <= 'z') || (ch>= 'A' && ch <= 'Z')) + { + hasCharacter = true; + sb.Append(ch); + } + else if (ch == '@') + { + sb.Append(ch); + } + else if (ch >= '0' && ch <= '9' && hasCharacter) + { + sb.Append(ch); + } + else + { + pos-= 2; + break; + } + } + + if (hasCharacter) + { + endIndex = pos; + return sb; + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractNumber.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractNumber.cs new file mode 100644 index 00000000..41c904eb --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractNumber.cs @@ -0,0 +1,85 @@ +using System.Text; + +public static partial class Extensions +{ + /// A StringBuilder extension method that extracts the number described by @this. + /// The @this to act on. + /// The extracted number. + public static StringBuilder ExtractNumber(this StringBuilder @this) + { + return @this.ExtractNumber(0); + } + + /// A StringBuilder extension method that extracts the number described by @this. + /// The @this to act on. + /// [out] The end index. + /// The extracted number. + public static StringBuilder ExtractNumber(this StringBuilder @this, out int endIndex) + { + return @this.ExtractNumber(0, out endIndex); + } + + /// A StringBuilder extension method that extracts the number described by @this. + /// The @this to act on. + /// The start index. + /// The extracted number. + public static StringBuilder ExtractNumber(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractNumber(startIndex, out endIndex); + } + + /// A StringBuilder extension method that extracts the number described by @this. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted number. + public static StringBuilder ExtractNumber(this StringBuilder @this, int startIndex, out int endIndex) + { + // WARNING: This method support all kind of suffix for .NET Runtime Compiler + // An operator can be any sequence of supported operator character + var sb = new StringBuilder(); + + var hasNumber = false; + var hasDot = false; + var hasSuffix = false; + + var pos = startIndex; + + while (pos < @this.Length) + { + var ch = @this[pos]; + pos++; + + if (ch >= '0' && ch <= '9' && !hasSuffix) + { + hasNumber = true; + sb.Append(ch); + } + else if (ch == '.' && !hasSuffix && !hasDot) + { + hasDot = true; + sb.Append(ch); + } + else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) + { + hasSuffix = true; + sb.Append(ch); + } + else + { + pos-= 2; + break; + } + } + + if (hasNumber) + { + endIndex = pos; + return sb; + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractOperator.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractOperator.cs new file mode 100644 index 00000000..6ab7b874 --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractOperator.cs @@ -0,0 +1,103 @@ +using System.Text; + +public static partial class Extensions +{ + /// A StringBuilder extension method that extracts the operator described by @this. + /// The @this to act on. + /// The extracted operator. + public static StringBuilder ExtractOperator(this StringBuilder @this) + { + return @this.ExtractOperator(0); + } + + /// A StringBuilder extension method that extracts the operator described by @this. + /// The @this to act on. + /// [out] The end index. + /// The extracted operator. + public static StringBuilder ExtractOperator(this StringBuilder @this, out int endIndex) + { + return @this.ExtractOperator(0, out endIndex); + } + + /// A StringBuilder extension method that extracts the operator described by @this. + /// The @this to act on. + /// The start index. + /// The extracted operator. + public static StringBuilder ExtractOperator(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractOperator(startIndex, out endIndex); + } + + /// A StringBuilder extension method that extracts the operator described by @this. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted operator. + public static StringBuilder ExtractOperator(this StringBuilder @this, int startIndex, out int endIndex) + { + // WARNING: This method support custom operator for .NET Runtime Compiler + // An operator can be any sequence of supported operator character + var sb = new StringBuilder(); + + var pos = startIndex; + + while (pos < @this.Length) + { + var ch = @this[pos]; + pos++; + + switch (ch) + { + case '`': + case '~': + case '!': + case '#': + case '$': + case '%': + case '^': + case '&': + case '*': + case '(': + case ')': + case '-': + case '_': + case '=': + case '+': + case '[': + case ']': + case '{': + case '}': + case '|': + case ':': + case ';': + case ',': + case '.': + case '<': + case '>': + case '?': + case '/': + sb.Append(ch); + break; + default: + if (sb.Length > 0) + { + endIndex = pos - 2; + return sb; + } + + endIndex = -1; + return null; + } + } + + if (sb.Length > 0) + { + endIndex = pos; + return sb; + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractString.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractString.cs new file mode 100644 index 00000000..5b4bf936 --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractString.cs @@ -0,0 +1,78 @@ +using System.Text; + +public static partial class Extensions +{ + /// A StringBuilder extension method that extracts the string described by @this. + /// The @this to act on. + /// The extracted string. + public static StringBuilder ExtractString(this StringBuilder @this) + { + return @this.ExtractString(0); + } + + /// A StringBuilder extension method that extracts the string described by @this. + /// The @this to act on. + /// [out] The end index. + /// The extracted string. + public static StringBuilder ExtractString(this StringBuilder @this, out int endIndex) + { + return @this.ExtractString(0, out endIndex); + } + + /// A StringBuilder extension method that extracts the string described by @this. + /// The @this to act on. + /// The start index. + /// The extracted string. + public static StringBuilder ExtractString(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractString(startIndex, out endIndex); + } + + /// A StringBuilder extension method that extracts the string described by @this. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted string. + public static StringBuilder ExtractString(this StringBuilder @this, int startIndex, out int endIndex) + { + if (@this.Length > startIndex + 1) + { + var ch1 = @this[startIndex]; + var ch2 = @this[startIndex + 1]; + + if (ch1 == '@' && ch2 == '"') + { + // @"my string" + + return @this.ExtractStringArobasDoubleQuote(startIndex, out endIndex); + } + + if (ch1 == '@' && ch2 == '\'') + { + // WARNING: This is not a valid string, however single quote is often used to make it more readable in text templating + // @'my string' + + return @this.ExtractStringArobasSingleQuote(startIndex, out endIndex); + } + + if (ch1 == '"') + { + // "my string" + + return @this.ExtractStringDoubleQuote(startIndex, out endIndex); + } + + if (ch1 == '\'') + { + // WARNING: This is not a valid string, however single quote is often used to make it more readable in text templating + // 'my string' + + return @this.ExtractStringSingleQuote(startIndex, out endIndex); + } + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringArobasDoubleQuote.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringArobasDoubleQuote.cs new file mode 100644 index 00000000..8ba31301 --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringArobasDoubleQuote.cs @@ -0,0 +1,95 @@ +using System; +using System.Text; + +public static partial class Extensions +{ + /// + /// A StringBuilder extension method that extracts the string arobas double quote + /// described by @this. + /// + /// The @this to act on. + /// The extracted string arobas double quote. + public static StringBuilder ExtractStringArobasDoubleQuote(this StringBuilder @this) + { + return @this.ExtractStringArobasDoubleQuote(0); + } + + /// + /// A StringBuilder extension method that extracts the string arobas double quote + /// described by @this. + /// + /// The @this to act on. + /// [out] The end index. + /// The extracted string arobas double quote. + public static StringBuilder ExtractStringArobasDoubleQuote(this StringBuilder @this, out int endIndex) + { + return @this.ExtractStringArobasDoubleQuote(0, out endIndex); + } + + /// + /// A StringBuilder extension method that extracts the string arobas double quote + /// described by @this. + /// + /// Thrown when an exception error condition occurs. + /// The @this to act on. + /// The start index. + /// The extracted string arobas double quote. + public static StringBuilder ExtractStringArobasDoubleQuote(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractStringArobasDoubleQuote(startIndex, out endIndex); + } + + /// + /// A StringBuilder extension method that extracts the string arobas double quote + /// described by @this. + /// + /// Thrown when an exception error condition occurs. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted string arobas double quote. + public static StringBuilder ExtractStringArobasDoubleQuote(this StringBuilder @this, int startIndex, out int endIndex) + { + var sb = new StringBuilder(); + + if (@this.Length > startIndex + 1) + { + var ch1 = @this[startIndex]; + var ch2 = @this[startIndex + 1]; + + if (ch1 == '@' && ch2 == '"') + { + // @"my string" + + var pos = startIndex + 2; + + while (pos < @this.Length) + { + var ch = @this[pos]; + pos++; + + if (ch == '"' && pos < @this.Length && @this[pos] == '"') + { + sb.Append(ch); + pos++; // Treat as escape character for @"abc""def" + } + else if (ch == '"') + { + endIndex = pos; + return sb; + } + else + { + sb.Append(ch); + } + } + + throw new Exception("Unclosed string starting at position: " + startIndex); + } + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringArobasSingleQuote.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringArobasSingleQuote.cs new file mode 100644 index 00000000..89ca5ccc --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringArobasSingleQuote.cs @@ -0,0 +1,90 @@ +using System; +using System.Text; + +public static partial class Extensions +{ + /// + /// A StringBuilder extension method that extracts the string arobas single quote + /// described by @this. + /// + /// The @this to act on. + /// The extracted string arobas single quote. + public static StringBuilder ExtractStringArobasSingleQuote(this StringBuilder @this) + { + return @this.ExtractStringArobasSingleQuote(0); + } + /// A StringBuilder extension method that extracts the string arobas single quote + /// described by @this. + /// The @this to act on. + /// [out] The end index. + /// The extracted string arobas single quote. + public static StringBuilder ExtractStringArobasSingleQuote(this StringBuilder @this, out int endIndex) + { + return @this.ExtractStringArobasSingleQuote(0, out endIndex); + } + + /// + /// A StringBuilder extension method that extracts the string arobas single quote + /// described by @this. + /// + /// Thrown when an exception error condition occurs. + /// The @this to act on. + /// The start index. + /// The extracted string arobas single quote. + public static StringBuilder ExtractStringArobasSingleQuote(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractStringArobasSingleQuote(startIndex, out endIndex); + } + /// A StringBuilder extension method that extracts the string arobas single quote + /// described by @this. + /// Thrown when an exception error condition occurs. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted string arobas single quote. + public static StringBuilder ExtractStringArobasSingleQuote(this StringBuilder @this, int startIndex, out int endIndex) + { + var sb = new StringBuilder(); + + if (@this.Length > startIndex + 1) + { + var ch1 = @this[startIndex]; + var ch2 = @this[startIndex + 1]; + + if (ch1 == '@' && ch2 == '\'') + { + // WARNING: This is not a valid string, however single quote is often used to make it more readable in text templating + // @'my string' + + var pos = startIndex + 2; + + while (pos < @this.Length) + { + var ch = @this[pos]; + pos++; + + if (ch == '\'' && pos < @this.Length && @this[pos] == '\'') + { + sb.Append(ch); + pos++; // Treat as escape character for @'abc''def' + } + else if (ch == '\'') + { + endIndex = pos; + return sb; + } + else + { + sb.Append(ch); + } + } + + throw new Exception("Unclosed string starting at position: " + startIndex); + } + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringDoubleQuote.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringDoubleQuote.cs new file mode 100644 index 00000000..47132a64 --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringDoubleQuote.cs @@ -0,0 +1,89 @@ +using System; +using System.Text; + +public static partial class Extensions +{ + /// + /// A StringBuilder extension method that extracts the string double quote described by + /// @this. + /// + /// The @this to act on. + /// The extracted string double quote. + public static StringBuilder ExtractStringDoubleQuote(this StringBuilder @this) + { + return @this.ExtractStringDoubleQuote(0); + } + /// A StringBuilder extension method that extracts the string double quote described by + /// @this. + /// The @this to act on. + /// [out] The end index. + /// The extracted string double quote. + public static StringBuilder ExtractStringDoubleQuote(this StringBuilder @this, out int endIndex) + { + return @this.ExtractStringDoubleQuote(0, out endIndex); + } + + /// + /// A StringBuilder extension method that extracts the string double quote described by + /// @this. + /// + /// Thrown when an exception error condition occurs. + /// The @this to act on. + /// The start index. + /// The extracted string double quote. + public static StringBuilder ExtractStringDoubleQuote(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractStringDoubleQuote(startIndex, out endIndex); + } + /// A StringBuilder extension method that extracts the string double quote described by + /// @this. + /// Thrown when an exception error condition occurs. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted string double quote. + public static StringBuilder ExtractStringDoubleQuote(this StringBuilder @this, int startIndex, out int endIndex) + { + var sb = new StringBuilder(); + + if (@this.Length > startIndex + 1) + { + var ch1 = @this[startIndex]; + + if (ch1 == '"') + { + // "my string" + + var pos = startIndex + 1; + + while (pos < @this.Length) + { + var ch = @this[pos]; + pos++; + + char nextChar; + if (ch == '\\' && pos < @this.Length && ((nextChar = @this[pos]) == '\\' || nextChar == '"')) + { + sb.Append(nextChar); + pos++; // Treat as escape character for \\ or \" + } + else if (ch == '"') + { + endIndex = pos; + return sb; + } + else + { + sb.Append(ch); + } + } + + throw new Exception("Unclosed string starting at position: " + startIndex); + } + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringSingleQuote.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringSingleQuote.cs new file mode 100644 index 00000000..f1b8ff46 --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractStringSingleQuote.cs @@ -0,0 +1,91 @@ +using System; +using System.Text; + +public static partial class Extensions +{ + /// + /// A StringBuilder extension method that extracts the string single quote described by + /// @this. + /// + /// The @this to act on. + /// The extracted string single quote. + public static StringBuilder ExtractStringSingleQuote(this StringBuilder @this) + { + return @this.ExtractStringSingleQuote(0); + } + /// A StringBuilder extension method that extracts the string single quote described by + /// @this. + /// The @this to act on. + /// [out] The end index. + /// The extracted string single quote. + public static StringBuilder ExtractStringSingleQuote(this StringBuilder @this, out int endIndex) + { + return @this.ExtractStringSingleQuote(0, out endIndex); + } + + + /// + /// A StringBuilder extension method that extracts the string single quote described by + /// @this. + /// + /// Thrown when an exception error condition occurs. + /// The @this to act on. + /// The start index. + /// The extracted string single quote. + public static StringBuilder ExtractStringSingleQuote(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractStringSingleQuote(startIndex, out endIndex); + } + /// A StringBuilder extension method that extracts the string single quote described by + /// @this. + /// Thrown when an exception error condition occurs. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted string single quote. + public static StringBuilder ExtractStringSingleQuote(this StringBuilder @this, int startIndex, out int endIndex) + { + var sb = new StringBuilder(); + + if (@this.Length > startIndex + 1) + { + var ch1 = @this[startIndex]; + + if (ch1 == '\'') + { + // WARNING: This is not a valid string, however single quote is often used to make it more readable in text templating + // 'my string' + + var pos = startIndex + 1; + + while (pos < @this.Length) + { + var ch = @this[pos]; + pos++; + + char nextChar; + if (ch == '\\' && pos < @this.Length && ((nextChar = @this[pos]) == '\\' || nextChar == '\'')) + { + sb.Append(nextChar); + pos++; // Treat as escape character for \\ or \" + } + else if (ch == '\'') + { + endIndex = pos; + return sb; + } + else + { + sb.Append(ch); + } + } + + throw new Exception("Unclosed string starting at position: " + startIndex); + } + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractToken.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractToken.cs new file mode 100644 index 00000000..e5ca90cb --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractToken.cs @@ -0,0 +1,129 @@ +using System; +using System.Text; + +public static partial class Extensions +{ + /// A StringBuilder extension method that extracts the directive described by @this. + /// The @this to act on. + /// The extracted directive. + public static StringBuilder ExtractToken(this StringBuilder @this) + { + return @this.ExtractToken(0); + } + + /// A StringBuilder extension method that extracts the directive described by @this. + /// The @this to act on. + /// [out] The end index. + /// The extracted directive. + public static StringBuilder ExtractToken(this StringBuilder @this, out int endIndex) + { + return @this.ExtractToken(0, out endIndex); + } + + /// A StringBuilder extension method that extracts the directive described by @this. + /// Thrown when an exception error condition occurs. + /// The @this to act on. + /// The start index. + /// The extracted directive. + public static StringBuilder ExtractToken(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractToken(startIndex, out endIndex); + } + + /// A StringBuilder extension method that extracts the directive described by @this. + /// Thrown when an exception error condition occurs. + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted directive. + public static StringBuilder ExtractToken(this StringBuilder @this, int startIndex, out int endIndex) + { + /* A token can be: + * - Keyword / Literal + * - Operator + * - String + * - Integer + * - Real + */ + + // CHECK first which type is the token + var ch1 = @this[startIndex]; + var pos = startIndex + 1; + + switch (ch1) + { + case '@': + if (pos < @this.Length && @this[pos] == '"') + { + return @this.ExtractStringArobasDoubleQuote(startIndex, out endIndex); + } + if (pos < @this.Length && @this[pos] == '\'') + { + return @this.ExtractStringArobasSingleQuote(startIndex, out endIndex); + } + + break; + case '"': + return @this.ExtractStringDoubleQuote(startIndex, out endIndex); + case '\'': + return @this.ExtractStringSingleQuote(startIndex, out endIndex); + case '`': + case '~': + case '!': + case '#': + case '$': + case '%': + case '^': + case '&': + case '*': + case '(': + case ')': + case '-': + case '_': + case '=': + case '+': + case '[': + case ']': + case '{': + case '}': + case '|': + case ':': + case ';': + case ',': + case '.': + case '<': + case '>': + case '?': + case '/': + return @this.ExtractOperator(startIndex, out endIndex); + case '0': + if (pos < @this.Length && (@this[pos] == 'x' || @this[pos] == 'X')) + { + return @this.ExtractHexadecimal(startIndex, out endIndex); + } + + return @this.ExtractNumber(startIndex, out endIndex); + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return @this.ExtractNumber(startIndex, out endIndex); + default: + if ((ch1 >= 'a' && ch1 <= 'z') || (ch1 >= 'A' && ch1 <= 'Z')) + { + return @this.ExtractKeyword(startIndex, out endIndex); + } + + endIndex = -1; + return null; + } + + throw new Exception("Invalid token"); + } +} \ No newline at end of file diff --git a/src/Z.Core/System.Text.StringBuilder/Extract/ExtractTriviaToken.cs b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractTriviaToken.cs new file mode 100644 index 00000000..376faef0 --- /dev/null +++ b/src/Z.Core/System.Text.StringBuilder/Extract/ExtractTriviaToken.cs @@ -0,0 +1,101 @@ +using System.Text; + +public static partial class Extensions +{ + /// + /// A StringBuilder extension method that extracts the trivia tokens described by + /// @this. + /// + /// The @this to act on. + /// The extracted trivia tokens. + public static StringBuilder ExtractTriviaToken(this StringBuilder @this) + { + return @this.ExtractTriviaToken(0); + } + + /// + /// A StringBuilder extension method that extracts the trivia tokens described by + /// @this. + /// + /// The @this to act on. + /// [out] The end index. + /// The extracted trivia tokens. + public static StringBuilder ExtractTriviaToken(this StringBuilder @this, out int endIndex) + { + return @this.ExtractTriviaToken(0, out endIndex); + } + + /// + /// A StringBuilder extension method that extracts the trivia tokens described by + /// @this. + /// + /// The @this to act on. + /// The start index. + /// The extracted trivia tokens. + public static StringBuilder ExtractTriviaToken(this StringBuilder @this, int startIndex) + { + int endIndex; + return @this.ExtractTriviaToken(startIndex, out endIndex); + } + + /// + /// A StringBuilder extension method that extracts the trivia tokens described by + /// @this. + /// + /// The @this to act on. + /// The start index. + /// [out] The end index. + /// The extracted trivia tokens. + public static StringBuilder ExtractTriviaToken(this StringBuilder @this, int startIndex, out int endIndex) + { + var sb = new StringBuilder(); + var pos = startIndex; + + var isSpace = false; + + while (pos < @this.Length) + { + var ch = @this[pos]; + pos++; + + if (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t') + { + isSpace = true; + sb.Append(ch); + } + else if (ch == '/' && !isSpace) + { + if (pos < @this.Length) + { + ch = @this[pos]; + if (ch == '/') + { + return @this.ExtractCommentSingleLine(startIndex, out endIndex); + } + if (ch == '*') + { + return @this.ExtractCommentMultiLine(startIndex, out endIndex); + } + + // otherwise is probably the divide operator + pos--; + break; + } + } + else + { + pos -= 2; + break; + } + } + + if (isSpace) + { + endIndex = pos; + return sb; + } + + endIndex = -1; + return null; + } +} \ No newline at end of file diff --git a/src/Z.Core/Z.Core.csproj b/src/Z.Core/Z.Core.csproj index d8b6a963..e5bd3b12 100644 --- a/src/Z.Core/Z.Core.csproj +++ b/src/Z.Core/Z.Core.csproj @@ -627,6 +627,21 @@ + + + + + + + + + + + + + + + diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractChar.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractChar.cs new file mode 100644 index 00000000..3fb00dfe --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractChar.cs @@ -0,0 +1,38 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractChar + { + [TestMethod] + public void ExtractChar() + { + int endIndex; + + // Unit Test + Assert.AreEqual('a', new StringBuilder("'a'").ExtractChar()); + Assert.AreEqual('\'', new StringBuilder("'\''").ExtractChar()); + Assert.AreEqual('\'', new StringBuilder("z'\''").ExtractChar(1, out endIndex)); + Assert.AreEqual(3, endIndex); + + try + { + new StringBuilder("'").ExtractChar(); + throw new Exception("invalid!"); + } + catch (Exception ex) + { + Assert.AreEqual("Invalid char at position: 0", ex.Message); + } + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractComment.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractComment.cs new file mode 100644 index 00000000..4da04dcc --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractComment.cs @@ -0,0 +1,36 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractComment + { + [TestMethod] + public void ExtractComment() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractComment()); + Assert.AreEqual("/*z", new StringBuilder("/*z").ExtractComment().ToString()); + Assert.AreEqual("/*z", new StringBuilder("/*/*z").ExtractComment(2).ToString()); + Assert.AreEqual("/*z*/", new StringBuilder("/**//*z*/").ExtractComment(4, out endIndex).ToString()); + Assert.AreEqual(8, endIndex); + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractComment()); + Assert.AreEqual("//z", new StringBuilder("//z").ExtractComment().ToString()); + Assert.AreEqual("//z", new StringBuilder("////z").ExtractComment(2).ToString()); + Assert.AreEqual("//z", new StringBuilder("////z" + Environment.NewLine + "z").ExtractComment(2, out endIndex).ToString()); + Assert.AreEqual(5, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractCommentMultiLine.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractCommentMultiLine.cs new file mode 100644 index 00000000..a3658641 --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractCommentMultiLine.cs @@ -0,0 +1,29 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractCommentMultiLine + { + [TestMethod] + public void ExtractCommentMultiLine() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractCommentMultiLine()); + Assert.AreEqual("/*z", new StringBuilder("/*z").ExtractCommentMultiLine().ToString()); + Assert.AreEqual("/*z", new StringBuilder("/*/*z").ExtractCommentMultiLine(2).ToString()); + Assert.AreEqual("/*z*/", new StringBuilder("/**//*z*/").ExtractCommentMultiLine(4, out endIndex).ToString()); + Assert.AreEqual(8, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractCommentSingleLine.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractCommentSingleLine.cs new file mode 100644 index 00000000..a684206d --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractCommentSingleLine.cs @@ -0,0 +1,29 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractCommentSingleLine + { + [TestMethod] + public void ExtractCommentSingleLine() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractCommentSingleLine()); + Assert.AreEqual("//z", new StringBuilder("//z").ExtractCommentSingleLine().ToString()); + Assert.AreEqual("//z", new StringBuilder("////z").ExtractCommentSingleLine(2).ToString()); + Assert.AreEqual("//z", new StringBuilder("////z" + Environment.NewLine + "z").ExtractCommentSingleLine(2, out endIndex).ToString()); + Assert.AreEqual(5, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractHexadecimal.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractHexadecimal.cs new file mode 100644 index 00000000..1003aca9 --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractHexadecimal.cs @@ -0,0 +1,28 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractHexadecimal + { + [TestMethod] + public void ExtractHexadecimal() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractHexadecimal()); + Assert.AreEqual("0x2A", new StringBuilder("0x2A").ExtractHexadecimal().ToString()); + Assert.AreEqual("0x2A", new StringBuilder("0x0x2A").ExtractHexadecimal(2).ToString()); + Assert.AreEqual("0x2FUL", new StringBuilder("0x0x2FUL").ExtractHexadecimal(2, out endIndex).ToString()); + Assert.AreEqual(8, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractKeyword.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractKeyword.cs new file mode 100644 index 00000000..c3dbc7fd --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractKeyword.cs @@ -0,0 +1,29 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractKeyword + { + [TestMethod] + public void ExtractKeyword() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractKeyword()); + Assert.AreEqual(null, new StringBuilder("1.1").ExtractKeyword()); + Assert.AreEqual(null, new StringBuilder("@1a").ExtractKeyword()); + Assert.AreEqual("for", new StringBuilder("for foreach").ExtractKeyword().ToString()); + Assert.AreEqual("@for", new StringBuilder("zzz @for foreach").ExtractKeyword(4, out endIndex).ToString()); + Assert.AreEqual(7, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractNumber.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractNumber.cs new file mode 100644 index 00000000..af9cd661 --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractNumber.cs @@ -0,0 +1,32 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractNumber + { + [TestMethod] + public void ExtractNumber() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractNumber()); + Assert.AreEqual(null, new StringBuilder(".").ExtractNumber()); + Assert.AreEqual("1", new StringBuilder("1").ExtractNumber().ToString()); + Assert.AreEqual(".1", new StringBuilder(".1").ExtractNumber().ToString()); + Assert.AreEqual(".1", new StringBuilder(".1.1").ExtractNumber().ToString()); + Assert.AreEqual(".1D", new StringBuilder(".1D").ExtractNumber().ToString()); + Assert.AreEqual(".1", new StringBuilder("1.1").ExtractNumber(1).ToString()); + Assert.AreEqual(".1F", new StringBuilder("1.1F1").ExtractNumber(1, out endIndex).ToString()); + Assert.AreEqual(3, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractOperator.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractOperator.cs new file mode 100644 index 00000000..ec9e6f1c --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractOperator.cs @@ -0,0 +1,31 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractOperator + { + [TestMethod] + public void ExtractOperator() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractOperator()); + Assert.AreEqual(null, new StringBuilder("a").ExtractOperator()); + Assert.AreEqual(null, new StringBuilder("1").ExtractOperator()); + Assert.AreEqual(".", new StringBuilder(".").ExtractOperator().ToString()); + Assert.AreEqual("..", new StringBuilder("..").ExtractOperator().ToString()); + Assert.AreEqual(".", new StringBuilder(". .").ExtractOperator().ToString()); + Assert.AreEqual("/*/", new StringBuilder("//*/ z").ExtractOperator(1, out endIndex).ToString()); + Assert.AreEqual(3, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractString.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractString.cs new file mode 100644 index 00000000..05039db7 --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractString.cs @@ -0,0 +1,45 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractString + { + [TestMethod] + public void ExtractString() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractStringArobasDoubleQuote()); + Assert.AreEqual("z", new StringBuilder("@\"z\"").ExtractStringArobasDoubleQuote().ToString()); + Assert.AreEqual("z", new StringBuilder("@@\"z\"z").ExtractStringArobasDoubleQuote(1, out endIndex).ToString()); + Assert.AreEqual(5, endIndex); + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractStringArobasSingleQuote()); + Assert.AreEqual("z", new StringBuilder("@'z'").ExtractStringArobasSingleQuote().ToString()); + Assert.AreEqual("z", new StringBuilder("@@'z'z").ExtractStringArobasSingleQuote(1, out endIndex).ToString()); + Assert.AreEqual(5, endIndex); + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractStringDoubleQuote()); + Assert.AreEqual("z", new StringBuilder("\"z\"").ExtractStringDoubleQuote().ToString()); + Assert.AreEqual("z", new StringBuilder("-\"z\"z").ExtractStringDoubleQuote(1, out endIndex).ToString()); + Assert.AreEqual(4, endIndex); + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractStringSingleQuote()); + Assert.AreEqual("z", new StringBuilder("'z'").ExtractStringSingleQuote().ToString()); + Assert.AreEqual("z", new StringBuilder("-'z'z").ExtractStringSingleQuote(1, out endIndex).ToString()); + Assert.AreEqual(4, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringArobasDoubleQuote.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringArobasDoubleQuote.cs new file mode 100644 index 00000000..bd27ea4f --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringArobasDoubleQuote.cs @@ -0,0 +1,27 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractStringArobasDoubleQuote + { + [TestMethod] + public void ExtractStringArobasDoubleQuote() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractStringArobasDoubleQuote()); + Assert.AreEqual("z", new StringBuilder("@\"z\"").ExtractStringArobasDoubleQuote().ToString()); + Assert.AreEqual("z", new StringBuilder("@@\"z\"z").ExtractStringArobasDoubleQuote(1, out endIndex).ToString()); + Assert.AreEqual(5, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringArobasSingleQuote.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringArobasSingleQuote.cs new file mode 100644 index 00000000..60f28d7e --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringArobasSingleQuote.cs @@ -0,0 +1,27 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractStringArobasSingleQuote + { + [TestMethod] + public void ExtractStringArobasSingleQuote() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractStringArobasSingleQuote()); + Assert.AreEqual("z", new StringBuilder("@'z'").ExtractStringArobasSingleQuote().ToString()); + Assert.AreEqual("z", new StringBuilder("@@'z'z").ExtractStringArobasSingleQuote(1, out endIndex).ToString()); + Assert.AreEqual(5, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringDoubleQuote.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringDoubleQuote.cs new file mode 100644 index 00000000..72273796 --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringDoubleQuote.cs @@ -0,0 +1,27 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractStringDoubleQuote + { + [TestMethod] + public void ExtractStringDoubleQuote() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractStringDoubleQuote()); + Assert.AreEqual("z", new StringBuilder("\"z\"").ExtractStringDoubleQuote().ToString()); + Assert.AreEqual("z", new StringBuilder("-\"z\"z").ExtractStringDoubleQuote(1, out endIndex).ToString()); + Assert.AreEqual(4, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringSingleQuote.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringSingleQuote.cs new file mode 100644 index 00000000..e3d8fce2 --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractStringSingleQuote.cs @@ -0,0 +1,27 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractStringSingleQuote + { + [TestMethod] + public void ExtractStringSingleQuote() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractStringSingleQuote()); + Assert.AreEqual("z", new StringBuilder("'z'").ExtractStringSingleQuote().ToString()); + Assert.AreEqual("z", new StringBuilder("-'z'z").ExtractStringSingleQuote(1, out endIndex).ToString()); + Assert.AreEqual(4, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractToken.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractToken.cs new file mode 100644 index 00000000..7f2512b4 --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractToken.cs @@ -0,0 +1,29 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractToken + { + [TestMethod] + public void ExtractToken() + { + int endIndex; + + // Unit Test + Assert.AreEqual(null, new StringBuilder(" ").ExtractToken()); + Assert.AreEqual("123.4LU", new StringBuilder("123.4LU foreach").ExtractToken().ToString()); + Assert.AreEqual("for", new StringBuilder("for foreach").ExtractToken().ToString()); + Assert.AreEqual("custom", new StringBuilder("custom foreach").ExtractToken().ToString()); + Assert.AreEqual("+=", new StringBuilder("z += 2").ExtractToken(2, out endIndex).ToString()); + Assert.AreEqual(3, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractTriviaToken.cs b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractTriviaToken.cs new file mode 100644 index 00000000..6d4cac43 --- /dev/null +++ b/test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.ExtractTriviaToken.cs @@ -0,0 +1,38 @@ +// Copyright (c) 2015 ZZZ Projects. All rights reserved +// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods) +// Website: http://www.zzzprojects.com/ +// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927 +// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library + +using System; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Core.Test +{ + [TestClass] + public class System_Text_StringBuilder_ExtractTriviaToken + { + [TestMethod] + public void ExtractTriviaToken() + { + int endIndex; + + // Unit Test + Assert.AreEqual("/*z", new StringBuilder("/*z").ExtractTriviaToken().ToString()); + Assert.AreEqual("/*z", new StringBuilder("/*/*z").ExtractTriviaToken(2).ToString()); + Assert.AreEqual("/*z*/", new StringBuilder("/**//*z*/").ExtractTriviaToken(4, out endIndex).ToString()); + Assert.AreEqual(8, endIndex); + + // Unit Test + Assert.AreEqual("//z", new StringBuilder("//z").ExtractTriviaToken().ToString()); + Assert.AreEqual("//z", new StringBuilder("////z").ExtractTriviaToken(2).ToString()); + Assert.AreEqual("//z", new StringBuilder("////z" + Environment.NewLine + "z").ExtractTriviaToken(2, out endIndex).ToString()); + Assert.AreEqual(5, endIndex); + + // UnitTest + Assert.AreEqual(" ", new StringBuilder(" z").ExtractTriviaToken(1, out endIndex).ToString()); + Assert.AreEqual(2, endIndex); + } + } +} \ No newline at end of file diff --git a/test/Z.Core.Test/Z.Core.Test.csproj b/test/Z.Core.Test/Z.Core.Test.csproj index 7c026de2..2128f386 100644 --- a/test/Z.Core.Test/Z.Core.Test.csproj +++ b/test/Z.Core.Test/Z.Core.Test.csproj @@ -221,6 +221,21 @@ + + + + + + + + + + + + + + +