PPiotr Fusik[release] 3.3.3.
dfef7a84创建于 5月23日历史提交
public static class HelloFu
{
	public const int VersionMajor = 3;
	public const int VersionMinor = 3;
	public const string Version = $"{VersionMajor}.{VersionMinor}";

	/// Returns `true` if and only if `x` is a power of 2 (1, 2, 4, 8, 16, ...).
	public static bool IsPowerOfTwo(int x)
	{
		return (x & x - 1) == 0 && x > 0;
	}

	/// Calculates greatest common divisor of `a` and `b`.
	public static int GreatestCommonDivisor(int a, int b)
	{
		// Euclidean algorithm
		while (b != 0) {
			int t = b;
			b = a % b;
			a = t;
		}
		return a;
	}

	/// Checks whether the given string is a palindrome.
	/// Note: empty string is considered palindrome.
	public static bool IsPalindrome(string s)
	{
		int j = s.Length;
		for (int i = 0; i < --j; i++)
			if (s[i] != s[j])
				return false;
		return true;
	}

	/// Gets a boolean value out of strings `"true"` and `"false"`.
	/// In other cases returns `defaultValue`.
	public static bool ParseBool(string s, bool defaultValue)
	{
		if (s == "true")
			return true;
		if (s == "false")
			return false;
		return defaultValue;
	}

	/// Converts an unsigned integer from its decimal representation.
	public static uint ParseUnsignedInt(string? s)
		/// Input is null or empty.
		throws Exception
	{
		if (s == null || s.Length == 0)
			throw Exception("Null or empty argument");
		uint r = 0;
		foreach (int c in s) {
			if (c < '0' || c > '9')
				throw Exception("Not a digit");
			if (r > 214748364 || (r == 214748364 && c >= '8'))
				throw Exception("Number too big");
			r = r * 10 + c - '0';
		}
		return r;
	}
}