/*
* Copyright (c) 2014-2017, Eren Okka
* Copyright (c) 2016-2017, Paul Miller
* Copyright (c) 2017-2018, Tyler Bratton
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
namespace AnitomySharp;
///
/// A string helper class that is analogous to string.cpp
of the original Anitomy, and StringHelper.java
of AnitomyJ.
///
public static class StringHelper
{
///
/// Returns whether or not the character is alphanumeric
///
public static bool IsAlphanumericChar(char c)
{
return c is >= '0' and <= '9' or >= 'A' and <= 'Z' or >= 'a' and <= 'z';
}
///
/// Returns whether or not the character is a hex character.
///
private static bool IsHexadecimalChar(char c)
{
return c is >= '0' and <= '9' or >= 'A' and <= 'F' or >= 'a' and <= 'f';
}
///
/// Returns whether or not the character is a latin character
///
private static bool IsLatinChar(char c)
{
// We're just checking until the end of the Latin Extended-B block,
// rather than all the blocks that belong to the Latin script.
return c <= '\u024F';
}
///
/// Returns whether or not the str
is a hex string.
///
public static bool IsHexadecimalString(string str)
{
return !string.IsNullOrEmpty(str) && str.All(IsHexadecimalChar);
}
///
/// Returns whether or not the str
is mostly a latin string.
///
public static bool IsMostlyLatinString(string str)
{
double length = !string.IsNullOrEmpty(str) ? 1.0 : str.Length;
return str.Where(IsLatinChar).Count() / length >= 0.5;
}
///
/// Returns whether or not the str
is a numeric string.
///
public static bool IsNumericString(string str)
{
return str.All(char.IsDigit);
}
///
/// Returns the int value of the str
; 0 otherwise.
///
public static int StringToInt(string str)
{
try
{
return int.Parse(str);
}
catch (Exception ex)
{
Console.WriteLine(ex);
return 0;
}
}
public static string SubstringWithCheck(string str, int start, int count)
{
if (start + count > str.Length) count = str.Length - start;
return str.Substring(start, count);
}
}