Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static System.Collections.Generic.IEnumerable<System.String> ReadWord( System.String input ) {
- input ??= System.String.Empty;
- var inlen = input.Length;
- System.Int32 start = 0;
- System.Int32 stop;
- System.Func<System.String, System.Int32, System.Int32, System.String> substr = null;
- do {
- while ( ( start < inlen ) && System.Char.IsWhiteSpace( input[ start ] ) ) {
- start++;
- }
- if ( start == inlen ) {
- yield break;
- } else if ( '(' == input[ start ] ) {
- stop = ReadString( input, start );
- substr = ( a, b, c ) => UnescapeString( a.Substring( b, c - b ) );
- } else {
- stop = ReadWord( input, start );
- substr = ( a, b, c ) => a.Substring( b, c - b );
- }
- if ( start == stop ) {
- yield break;
- } else {
- yield return substr( input, start, stop - start );
- }
- start = stop;
- } while ( start < inlen );
- yield break;
- }
- private static System.String UnescapeString( System.String input ) {
- var sb = new System.Text.StringBuilder( input.Length );
- System.Int32 i = -1;
- System.Char c;
- while ( ++i < input.Length ) {
- c = input[ i ];
- if ( '\\' == c ) {
- switch ( input[ ++i ] ) {
- case '0':
- c = '\0';
- break;
- case 'a':
- c = '\a';
- break;
- case 'b':
- c = '\b';
- break;
- case 'f':
- c = '\f';
- break;
- case 'r':
- c = '\r';
- break;
- case 'n':
- c = '\n';
- break;
- case 't':
- c = '\t';
- break;
- case 'v':
- c = '\v';
- break;
- default:
- c = input[ i ];
- break;
- }
- }
- sb = sb.Append( c );
- }
- return sb.ToString();
- }
- private static System.Int32 ReadWord( System.String input, System.Int32 pos ) {
- var inlen = input.Length;
- while ( ( pos < inlen ) && !System.Char.IsWhiteSpace( input[ pos ] ) ) {
- pos++;
- }
- return pos;
- }
- private static System.Int32 ReadString( System.String input, System.Int32 pos ) {
- var inlen = input.Length;
- while ( ( pos < inlen ) && ( ')' != input[ pos ] ) ) {
- if ( '\\' == input[ pos ] ) {
- pos++;
- }
- pos++;
- }
- pos++;
- if ( inlen < pos ) {
- throw new System.InvalidOperationException( "Unterminated string" );
- }
- return pos;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement