uniblab icon

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

uniblab | PRO | 12/05/20 12:18:29 AM UTC | 0 ⭐ | 2267 👁️ | Never ⏰ | []
C# |

1.22 KB

|

None

|

0 👍

/

0 👎

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;
            do {
                while ( ( start < inlen ) && System.Char.IsWhiteSpace( input[ start ] ) ) {
                    start++;
                }
                if ( start == inlen ) {
                    throw new System.ApplicationException();
                } else if ( '(' == input[ start ] ) {
                    stop = ReadString( input, start );
                } else {
                    stop = ReadWord( input, start );
                }
                if ( start == stop ) {
                    yield break;
                } else {
                    yield return input.Substring( start, stop - start );
                }
                start = stop;
            } while ( start < inlen );
 
            yield break;
        }
        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++;
            }
            return ++pos;
        }
 

Comments