/// <summary> /// Creates a list of zero or more segments representing the specified text string. /// <para> /// The text may contain the full range of Unicode characters. /// </para> /// <para> /// The result may multiple segments with various encoding modes in order to minimize the length of the bit stream. /// </para> /// </summary> /// <param name="text">The text to be encoded.</param> /// <returns>The created mutable list of segments representing the specified text.</returns> /// <exception cref="ArgumentNullException"><c>text</c> is <c>null</c>.</exception> /// <remarks> /// The current implementation does not use multiple segments. /// </remarks> public static List <QrSegment> MakeSegments(string text) { Objects.RequireNonNull(text); // Select the most efficient segment encoding automatically var result = new List <QrSegment>(); if (text == "") { // Leave result empty } else if (NumericRegex.IsMatch(text)) { result.Add(MakeNumeric(text)); } else if (AlphanumericRegex.IsMatch(text)) { result.Add(MakeAlphanumeric(text)); } else { result.Add(MakeBytes(Encoding.UTF8.GetBytes(text))); } return(result); }
/// <summary> /// Creates a segment representing the specified string of decimal digits. /// The segment is encoded in numeric mode. /// </summary> /// <param name="digits">The text to encode, consisting of digits from 0 to 9 only.</param> /// <returns>The created segment containing the text.</returns> /// <exception cref="ArgumentNullException"><c>digits</c> is <c>null</c>.</exception> /// <exception cref="ArgumentOutOfRangeException"><c>digits</c> contains non-digit characters</exception> public static QrSegment MakeNumeric(string digits) { if (!NumericRegex.IsMatch(digits)) { throw new ArgumentOutOfRangeException(nameof(digits), "String contains non-numeric characters"); } BitArray ba = new BitArray(0); for (int i = 0; i < digits.Length;) { // Consume up to 3 digits per iteration int n = Math.Min(digits.Length - i, 3); ba.AppendBits(uint.Parse(digits.Substring(i, n)), n * 3 + 1); i += n; } return(new QrSegment(Mode.Numeric, digits.Length, ba)); }
private bool IsNumeric(string numericStr) { return(NumericRegex.IsMatch(numericStr)); }
/// <summary> /// Tests whether the specified string can be encoded as a segment in numeric mode. /// <para> /// A string is encodable iff each character is in the range "0" to "9". /// </para> /// </summary> /// <param name="text">the string to test for encodability (not <c>null</c>)</param> /// <returns><c>true</c> iff each character is in the range "0" to "9".</returns> /// <exception cref="NullReferenceException">if the string is <c>null</c></exception> /// <seealso cref="MakeNumeric(string)"/> public static bool IsNumeric(string text) { return(NumericRegex.IsMatch(text)); }