Advertisement
uniblab

Reading words & strings encapsulated by ( and ). Not very good.. yet

Dec 4th, 2020
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. public static System.Collections.Generic.IEnumerable<System.String> ReadWord( System.String input ) {
  2.             input ??= System.String.Empty;
  3.  
  4.             var inlen = input.Length;
  5.             System.Int32 start = 0;
  6.             System.Int32 stop;
  7.             do {
  8.                 while ( ( start < inlen ) && System.Char.IsWhiteSpace( input[ start ] ) ) {
  9.                     start++;
  10.                 }
  11.                 if ( start == inlen ) {
  12.                     throw new System.ApplicationException();
  13.                 } else if ( '(' == input[ start ] ) {
  14.                     stop = ReadString( input, start );
  15.                 } else {
  16.                     stop = ReadWord( input, start );
  17.                 }
  18.                 if ( start == stop ) {
  19.                     yield break;
  20.                 } else {
  21.                     yield return input.Substring( start, stop - start );
  22.                 }
  23.                 start = stop;
  24.             } while ( start < inlen );
  25.  
  26.             yield break;
  27.         }
  28.         private static System.Int32 ReadWord( System.String input, System.Int32 pos ) {
  29.             var inlen = input.Length;
  30.             while ( ( pos < inlen ) && !System.Char.IsWhiteSpace( input[ pos ] ) ) {
  31.                 pos++;
  32.             }
  33.             return pos;
  34.         }
  35.         private static System.Int32 ReadString( System.String input, System.Int32 pos ) {
  36.             var inlen = input.Length;
  37.             while ( ( pos < inlen ) && ( ')' != input[ pos ] ) ) {
  38.                 if ( '\\' == input[ pos ] ) {
  39.                     pos++;
  40.                 }
  41.                 pos++;
  42.             }
  43.             return ++pos;
  44.         }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement