Toolkit.UI.Controls.Avalonia
This commit is contained in:
+57
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Gma.QrCodeNet.Encoding.DataEncodation.InputRecognition;
|
||||
|
||||
public static class InputRecognise
|
||||
{
|
||||
public static RecognitionStruct Recognise(string content)
|
||||
{
|
||||
string encodingName = EightBitByteRecognision(content, 0, content.Length);
|
||||
return new RecognitionStruct(encodingName);
|
||||
}
|
||||
|
||||
private static string EightBitByteRecognision(string content, int startPos, int contentLength)
|
||||
{
|
||||
if(string.IsNullOrEmpty(content))
|
||||
throw new ArgumentNullException(nameof(content));
|
||||
|
||||
var eciSets = new ECISet(ECISet.AppendOption.NameToValue);
|
||||
|
||||
Dictionary<string, int>? eciSet = eciSets.GetECITable();
|
||||
|
||||
if(eciSet == null)
|
||||
return string.Empty;
|
||||
|
||||
// we will not check for utf8 encoding.
|
||||
eciSet.Remove(QRCodeConstantVariable.UTF8Encoding);
|
||||
eciSet.Remove(QRCodeConstantVariable.DefaultEncoding);
|
||||
|
||||
int scanPos = startPos;
|
||||
|
||||
// default encoding as priority
|
||||
scanPos = ModeEncodeCheck.TryEncodeEightBitByte(content, QRCodeConstantVariable.DefaultEncoding, scanPos, contentLength);
|
||||
if (scanPos == -1)
|
||||
{
|
||||
return QRCodeConstantVariable.DefaultEncoding;
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, int> kvp in eciSet)
|
||||
{
|
||||
scanPos = ModeEncodeCheck.TryEncodeEightBitByte(content, kvp.Key, scanPos, contentLength);
|
||||
if (scanPos == -1)
|
||||
{
|
||||
return kvp.Key;
|
||||
}
|
||||
}
|
||||
|
||||
if (scanPos == -1)
|
||||
{
|
||||
throw new ArgumentException("foreach Loop check give wrong result.");
|
||||
}
|
||||
else
|
||||
{
|
||||
return QRCodeConstantVariable.UTF8Encoding;
|
||||
}
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
|
||||
namespace Gma.QrCodeNet.Encoding.DataEncodation.InputRecognition;
|
||||
|
||||
public static class ModeEncodeCheck
|
||||
{
|
||||
/// <summary>
|
||||
/// Encoding.GetEncoding.GetBytes will transform char to 0x3F if that char not belong to current encoding table.
|
||||
/// 0x3F is '?'
|
||||
/// </summary>
|
||||
private const int QuestionMarkChar = 0x3F;
|
||||
|
||||
/// <summary>
|
||||
/// Use given encoding to check input string from starting position. If encoding table is suitable solution.
|
||||
/// it will return -1. Else it will return failed encoding position.
|
||||
/// </summary>
|
||||
/// <param name="content">Input string</param>
|
||||
/// <param name="encodingName">Encoding name. Check ECI table</param>
|
||||
/// <returns>Returns -1 if from starting position to end encoding success. Else returns fail position</returns>
|
||||
internal static int TryEncodeEightBitByte(string content, string encodingName, int startingPosition, int contentLength)
|
||||
{
|
||||
if (string.IsNullOrEmpty(content))
|
||||
{
|
||||
throw new IndexOutOfRangeException("Input cannot be null or empty.");
|
||||
}
|
||||
|
||||
System.Text.Encoding encoding;
|
||||
try
|
||||
{
|
||||
encoding = System.Text.Encoding.GetEncoding(encodingName);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return startingPosition;
|
||||
}
|
||||
|
||||
char[] currentChar = new char[1];
|
||||
byte[] bytes;
|
||||
|
||||
for (int index = startingPosition; index < contentLength; index++)
|
||||
{
|
||||
currentChar[0] = content[index];
|
||||
bytes = encoding.GetBytes(currentChar);
|
||||
int length = bytes.Length;
|
||||
if (currentChar[0] != '?' && length == 1 && bytes[0] == QuestionMarkChar)
|
||||
{
|
||||
return index;
|
||||
}
|
||||
else if (length > 1)
|
||||
{
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
for (int index = 0; index < startingPosition; index++)
|
||||
{
|
||||
currentChar[0] = content[index];
|
||||
bytes = encoding.GetBytes(currentChar);
|
||||
int length = bytes.Length;
|
||||
if (currentChar[0] != '?' && length == 1 && bytes[0] == QuestionMarkChar)
|
||||
{
|
||||
return index;
|
||||
}
|
||||
else if (length > 1)
|
||||
{
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
namespace Gma.QrCodeNet.Encoding.DataEncodation.InputRecognition;
|
||||
|
||||
public struct RecognitionStruct
|
||||
{
|
||||
public RecognitionStruct(string encodingName)
|
||||
: this()
|
||||
{
|
||||
EncodingName = encodingName;
|
||||
}
|
||||
|
||||
public string EncodingName { get; private set; }
|
||||
}
|
||||
Reference in New Issue
Block a user