Skip to content

Commit

Permalink
v12.1.0 - Add some TextReader extension methods in the spirit of dotn…
Browse files Browse the repository at this point in the history
…et/runtime#64621

Incomplete unit tests added for this
Also some methods to import statically for the same reading features on the Console.In
  • Loading branch information
monoman committed Jan 17, 2023
1 parent 3f67833 commit 0d3fa7d
Show file tree
Hide file tree
Showing 63 changed files with 293 additions and 62 deletions.
15 changes: 14 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
tab_width = 4
dotnet_style_operator_placement_when_wrapping = end_of_line
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_explicit_tuple_names = true:warning
dotnet_style_prefer_inferred_tuple_names = true:suggestion

[*.{c,c++,cc,cp,cpp,cu,cuh,cxx,h,hh,hpp,hxx,inc,inl,ino,ipp,mpp,proto,tpp}]
indent_size = tab
Expand Down Expand Up @@ -221,4 +233,5 @@ dotnet_style_qualification_for_property = false:silent
dotnet_style_readonly_field = true:warning
dotnet_style_require_accessibility_modifiers = always:silent

file_header_template = ******************************************************************************************************************************\n \nCopyright (c) 2018-2022 InterlockLedger Network\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met\n\n* Redistributions of source code must retain the above copyright notice, this\n list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice,\n this list of conditions and the following disclaimer in the documentation\n and/or other materials provided with the distribution.\n\n* Neither the name of the copyright holder nor the names of its\n contributors may be used to endorse or promote products derived from\n this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n******************************************************************************************************************************
file_header_template = ******************************************************************************************************************************\n \nCopyright (c) 2018-2023 InterlockLedger Network\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met\n\n* Redistributions of source code must retain the above copyright notice, this\n list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice,\n this list of conditions and the following disclaimer in the documentation\n and/or other materials provided with the distribution.\n\n* Neither the name of the copyright holder nor the names of its\n contributors may be used to endorse or promote products derived from\n this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n******************************************************************************************************************************
csharp_style_prefer_top_level_statements = true:silent
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>preview</LangVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ******************************************************************************************************************************

#nullable enable

namespace System.IO;

[TestFixture]
public class TextReaderExtensionsTests
{
[TestCase("123", 123)]
[TestCase(" 123", 123)]
[TestCase(" 123 ", 123)]
[TestCase(" +123", 123)]
[TestCase("-123", -123)]
[TestCase(" -123", -123)]
[TestCase(" -123 ", -123)]
[TestCase("2000000000", 2000000000)]
[TestCase("-2000000000", -2000000000)]
public void TestReadInt32(string input, int expected) => Assert.AreEqual(expected, new StringReader(input).ReadInt32());

[TestCase("")]
[TestCase("1B2C3")]
[TestCase("123Z")]
[TestCase("-+123")]
[TestCase("2500000000")]
[TestCase("-2500000000")]
public void TestReadInt32Failing(string input) {
var ex = Assert.Throws<InvalidOperationException>(() => new StringReader(input).ReadInt32());
TestContext.WriteLine(ex);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -33,7 +33,6 @@
#nullable enable

namespace System;

[TestFixture]
public class LimitedRangeTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Commons/AssemblyAttributes.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
110 changes: 110 additions & 0 deletions InterlockLedger.Commons/Extensions/System.IO/TextReaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ******************************************************************************************************************************

namespace System.IO;
public static class TextReaderExtensions
{
public static sbyte ReadInt8(this TextReader reader) => ReadNumber(reader, (ulong)sbyte.MaxValue, v => (sbyte)v, true);
public static short ReadInt16(this TextReader reader) => ReadNumber(reader, (ulong)short.MaxValue, v => (short)v, true);
public static int ReadInt32(this TextReader reader) => ReadNumber(reader, int.MaxValue, v => (int)v, true);
public static long ReadInt64(this TextReader reader) => ReadNumber(reader, long.MaxValue, v => (long)v, true);
public static byte ReadUInt8(this TextReader reader) => ReadNumber(reader, byte.MaxValue, v => (byte)v, false);
public static ushort ReadUInt16(this TextReader reader) => ReadNumber(reader, ushort.MaxValue, v => (ushort)v, false);
public static uint ReadUInt32(this TextReader reader) => ReadNumber(reader, uint.MaxValue, v => (uint)v, false);
public static ulong ReadUInt64(this TextReader reader) => ReadNumber(reader, ulong.MaxValue, v => v, false);
public static string ReadToken(this TextReader reader) {
StringBuilder sb = new();
const string WhatImReading = "token";
char ch = CheckChar(reader.SkipWhiteSpace(), WhatImReading);
while (true) {
_ = sb.Append(ch);
char? next = reader.ReadChar();
if (next is null || char.IsWhiteSpace(next.Value))
break;
ch = next.Value;
}
return sb.ToString();
}


public static char? SkipWhiteSpace(this TextReader reader) {
char? ch;
do ch = reader.ReadChar();
while (ch.HasValue && char.IsWhiteSpace(ch.Value));
return ch;
}

public static char? ReadChar(this TextReader reader) {
int ch = reader.Read();
return ch >= 0 ? (char?)ch : null;
}


private static char CheckChar(char? ch, string what) =>
ch ?? throw new InvalidOperationException($"Could not find a {what} to read");

private static T ReadNumber<T>(TextReader reader, ulong maxValue, Func<ulong, T> cast, bool signed) where T : Numerics.INumber<T> {
const string WhatImReading = "number";
ulong value = 0;
bool isNegative = false;
char ch = CheckChar(reader.SkipWhiteSpace(), WhatImReading);
if (ch.In('-', '+')) {
if (signed) {
isNegative = ch == '-';
ch = CheckChar(reader.ReadChar(), WhatImReading);
} else
throw new InvalidOperationException($"Unexpected sign character '{ch}' in unsigned number");

}
while (true) {
if (!char.IsAsciiDigit(ch)) {
if (char.IsWhiteSpace(ch))
break;
else
throw new InvalidOperationException($"Invalid character '{ch}' in number");
} else {
value = value * 10 + (byte)(ch - '0');
char? digit = reader.ReadChar();
if (digit is null || char.IsWhiteSpace(digit.Value))
break;
ch = digit.Value;
}
}
if (value > maxValue)
throw new InvalidOperationException("Number is too big");
else {
T castValue = cast(value);
return isNegative ? -castValue : castValue;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Loading

0 comments on commit 0d3fa7d

Please sign in to comment.