Skip to content

Instantly share code, notes, and snippets.

@jamierocks
Created December 8, 2020 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamierocks/3442ced486116d957bb89ab77d2c40d8 to your computer and use it in GitHub Desktop.
Save jamierocks/3442ced486116d957bb89ab77d2c40d8 to your computer and use it in GitHub Desktop.
C# port of my Java string library (https://github.com/jamiemansfield/string)
/*
* This file is part of string, licensed under the MIT License (MIT).
*
* Copyright (c) Jamie Mansfield <https://www.jamiemansfield.me/>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace AOC2020.Utils
{
/// <summary>
/// Reader for strings.
/// </summary>
public class StringReader
{
public StringReader(string source)
{
Source = source;
}
public string Source { get; }
/// <summary>
/// The length of the source string.
/// </summary>
public int Length => Source.Length;
/// <summary>
/// The index (position) of the character currently being read.
/// </summary>
public int Index { get; private set; }
/// <summary>
/// The count of remaining characters to be read from the source.
/// </summary>
public int Remaining => Length - Index;
/// <summary>
/// Establishes whether a character is available to be read in the given offset.
/// </summary>
/// <param name="offset">How many characters ahead of the index (defaults to 1)</param>
/// <returns><code>true</code> if there is a character to read; <code>false</code> otherwise</returns>
public bool Available(int offset = 1) => Index + offset <= Length;
/// <summary>
/// Advances the index by the given length.
/// </summary>
/// <param name="length">How much to advance by</param>
public void Skip(int length) => Index += length;
/// <summary>
/// Gets the character at the index and offset, without incrementing the index.
/// </summary>
/// <param name="offset">How many characters ahead of the index (defaults to 0)</param>
/// <returns>The character</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="offset"/> is out of range</exception>
public char Peek(int offset = 0) => Source[Index + Verify(offset)];
/// <summary>
/// Gets the character at the index and offset, incrementing the index.
/// </summary>
/// <param name="offset">How many characters ahead of the index (defaults to 0)</param>
/// <returns>The character</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="offset"/> is out of range</exception>
public char Advance(int offset = 0) => Source[(Index += Verify(offset) + 1) - 1];
/// <summary>
/// Gets a substring from the source.
/// </summary>
/// <param name="start">The starting index</param>
/// <param name="end">The ending index</param>
/// <returns>The substring</returns>
public string Substring(int start, int end) => Source.Substring(start, end - start);
private int Verify(int offset)
{
if (Available(offset)) return offset;
throw new ArgumentOutOfRangeException(nameof(offset), "No character available");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment