RTools.Util.CharBuffer.IndexOf C# (CSharp) Method

IndexOf() public method

Find the first instance of a character in the buffer, and return its index. This returns -1 if the character is not found.
public IndexOf ( char c ) : int
c char The character to find.
return int
        public int IndexOf(char c)
        {
            for (int i = headIndex; i < tailIndex; i++)
            {
                if (buffer[i] == c) return (i - headIndex);
            }
            return (-1);
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Simple self test.
        /// </summary>
        /// <returns>bool - true for test passed, false otherwise</returns>
        public static bool TestSelf()
        {
            Logger log = new Logger("CharBuffer: TestSelf");
            log.Info("Starting...");

            // Append
            CharBuffer cb = new CharBuffer();
            cb.Append('a'); cb.Append('b'); cb.Append('c');
            log.Info("cb after Append: '{0}'", cb);
            if (cb[0] != 'a') { log.Error("Append or indexer failed."); return (false); }
            if (cb[1] != 'b') { log.Error("Append or indexer failed."); return (false); }
            if (cb[2] != 'c') { log.Error("Append or indexer failed."); return (false); }

            // Append string
            cb.Append("_hello");
            log.Info("cb after Append string: '{0}'", cb);
            if (cb[4] != 'h') { log.Error("Append or indexer failed."); return (false); }

            // Clear
            cb.Clear();
            if (cb.Length != 0) { log.Error("Clear failed."); return (false); }
            log.Info("cb after Clear: '{0}'", cb);

            // Grow
            cb = new CharBuffer(0);
            for (int i = 0; i < 33; i++) cb.Append('a');
            log.Info("cb after Growth: '{0}'", cb);
            if (cb[32] != 'a') { log.Error("Append or indexer failed."); return (false); }

            // IndexOf
            cb.Clear();
            cb.Append("This is a sentence");
            if (cb.IndexOf('a') != 8) { log.Error("IndexOf failed."); return (false); }

            // remove
            cb.Remove(0);
            log.Info("cb after Remove: '{0}'", cb);
            if (cb.IndexOf('a') != 7) { log.Error("IndexOf failed."); return (false); }

            cb.Remove(1);
            log.Info("cb after Remove: '{0}'", cb);
            if (cb.IndexOf('i') != 3) { log.Error("IndexOf failed."); return (false); }

            cb.Remove(2, 4);
            log.Info("cb after Remove: '{0}'", cb);
            if (cb[4] != 's') { log.Error("IndexOf failed."); return (false); }

            // use as a ring
            log.Info("Test ring buffer:");
            cb = new CharBuffer(16);
            for (int i = 0; i < 32; i++)
            {
                cb.Append("hello");
                if (!cb.ToString().Equals("hello")) { log.Error("Not hello after append."); return (false); }
                cb.Remove(0, 5);
                if (cb.Length != 0) { log.Error("Len wrong after remove."); return (false); }
            }

            log.Info("Done.");
            return (true);
        }
All Usage Examples Of RTools.Util.CharBuffer::IndexOf