138 lines
6.1 KiB
C#
138 lines
6.1 KiB
C#
using System.Text;
|
|
using View_by_Distance.Shared.Models.Stateless.Methods;
|
|
|
|
namespace View_by_Distance.Shared.Models.Stateless;
|
|
|
|
internal abstract class Id
|
|
{
|
|
|
|
internal static bool NameWithoutExtensionIsIdFormat(string fileNameFirstSegment)
|
|
{
|
|
bool result;
|
|
int intMinValueLength = int.MinValue.ToString().Length;
|
|
if (fileNameFirstSegment.Length < 5 || fileNameFirstSegment.Length > intMinValueLength)
|
|
result = false;
|
|
else
|
|
{
|
|
bool skipOneAllAreNumbers = fileNameFirstSegment[1..].All(l => char.IsNumber(l));
|
|
result = (skipOneAllAreNumbers && fileNameFirstSegment[0] == '-') || (skipOneAllAreNumbers && char.IsNumber(fileNameFirstSegment[0]));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static int GetId(MetadataConfiguration metadataConfiguration, string intelligentId)
|
|
{
|
|
int result;
|
|
StringBuilder results = new();
|
|
if (metadataConfiguration.IntMinValueLength < 4)
|
|
throw new NotSupportedException();
|
|
for (int j = intelligentId.Length - 4; j > -1; j--)
|
|
_ = results.Append(intelligentId[j]);
|
|
_ = results.Append(intelligentId[^3]).Append(intelligentId[^2]);
|
|
result = int.Parse(results.ToString());
|
|
if (intelligentId[^1] is '1' or '2')
|
|
result *= -1;
|
|
else if (intelligentId[^1] is not '9' and not '8')
|
|
throw new NotSupportedException();
|
|
return result;
|
|
}
|
|
|
|
private static IntelligentIdRecord GetIntelligentIdRecord(MetadataConfiguration metadataConfiguration, long id, bool ignore)
|
|
{
|
|
IntelligentIdRecord result;
|
|
StringBuilder stringBuilder = new();
|
|
if (metadataConfiguration.IntMinValueLength < 4)
|
|
throw new NotSupportedException();
|
|
int key;
|
|
string value;
|
|
if (id > -1)
|
|
{
|
|
key = ignore ? 8 : 9;
|
|
value = id.ToString().PadLeft(metadataConfiguration.IntMinValueLength, '0');
|
|
}
|
|
else
|
|
{
|
|
key = ignore ? 2 : 1;
|
|
value = id.ToString()[1..].PadLeft(metadataConfiguration.IntMinValueLength, '0');
|
|
}
|
|
for (int j = value.Length - 3; j > -1; j--)
|
|
_ = stringBuilder.Append(value[j]);
|
|
result = new(key, value[^2], value[^1], stringBuilder.ToString());
|
|
return result;
|
|
}
|
|
|
|
private static string GetIntelligentId(IntelligentIdRecord intelligentId) =>
|
|
$"{intelligentId.Reverse}{intelligentId.GroupChar2}{intelligentId.GroupChar1}{intelligentId.Key}";
|
|
|
|
internal static string GetPaddedId(MetadataConfiguration metadataConfiguration, int index, int id)
|
|
{
|
|
string result;
|
|
IntelligentIdRecord intelligentIdRecord = GetIntelligentIdRecord(metadataConfiguration, id, ignore: false);
|
|
string intelligentId = GetIntelligentId(intelligentIdRecord);
|
|
int check = GetId(metadataConfiguration, intelligentId);
|
|
if (check != id)
|
|
throw new NotSupportedException();
|
|
result = $"{metadataConfiguration.Offset + index}{intelligentId}";
|
|
return result;
|
|
}
|
|
|
|
internal static FilePath GetFilePath(MetadataConfiguration metadataConfiguration, string file, int? index)
|
|
{
|
|
FilePath result;
|
|
int? id;
|
|
int? sortOder;
|
|
string fileName = Path.GetFileName(file);
|
|
string[] segments = Path.GetFileName(fileName).Split('.');
|
|
string fileExtensionLowered = Path.GetExtension(file).ToLower();
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
|
string fileDirectoryName = Path.GetDirectoryName(file) ?? throw new NullReferenceException();
|
|
short sortOrderOnlyLengthIndex = IId.GetSortOrderOnlyLengthIndex(metadataConfiguration);
|
|
string fileNameFirstSegment = segments[0];
|
|
bool fileNameFirstSegmentIsIntelligentIdFormat = IId.NameWithoutExtensionIsIntelligentIdFormat(metadataConfiguration, fileNameFirstSegment);
|
|
bool fileNameFirstSegmentIsPaddedIntelligentIdFormat = IId.NameWithoutExtensionIsPaddedIntelligentIdFormat(metadataConfiguration, sortOrderOnlyLengthIndex, fileNameFirstSegment);
|
|
bool fileNameFirstSegmentIsIdFormat = !fileNameFirstSegmentIsPaddedIntelligentIdFormat && !fileNameFirstSegmentIsIntelligentIdFormat && IId.NameWithoutExtensionIsIdFormat(fileNameFirstSegment);
|
|
if (fileNameFirstSegmentIsIdFormat)
|
|
{
|
|
if (index is null)
|
|
throw new NullReferenceException(nameof(index));
|
|
if (!int.TryParse(fileNameFirstSegment, out int valueOfFileNameFirstSegment))
|
|
throw new NotSupportedException();
|
|
(id, sortOder) = (valueOfFileNameFirstSegment, metadataConfiguration.Offset + index);
|
|
}
|
|
else if (!fileNameFirstSegmentIsIntelligentIdFormat && !fileNameFirstSegmentIsPaddedIntelligentIdFormat)
|
|
(id, sortOder) = (null, null);
|
|
else if (fileNameFirstSegmentIsIntelligentIdFormat)
|
|
(id, sortOder) = (GetId(metadataConfiguration, fileNameFirstSegment), null);
|
|
else if (fileNameFirstSegmentIsPaddedIntelligentIdFormat)
|
|
{
|
|
if (!int.TryParse(fileNameFirstSegment[..sortOrderOnlyLengthIndex], out int absoluteValueOfSortOrder))
|
|
(id, sortOder) = (null, null);
|
|
else
|
|
(id, sortOder) = (GetId(metadataConfiguration, fileNameFirstSegment[sortOrderOnlyLengthIndex..]), absoluteValueOfSortOrder);
|
|
}
|
|
else
|
|
throw new NotSupportedException();
|
|
result = new(fileDirectoryName, fileExtensionLowered, fileNameFirstSegment, file, id, fileNameFirstSegmentIsIntelligentIdFormat, fileName, fileNameWithoutExtension, sortOder);
|
|
return result;
|
|
}
|
|
|
|
internal static int GetDeterministicHashCode(byte[] value)
|
|
{
|
|
int result;
|
|
unchecked
|
|
{
|
|
int hash1 = (5381 << 16) + 5381;
|
|
int hash2 = hash1;
|
|
for (int i = 0; i < value.Length; i += 2)
|
|
{
|
|
hash1 = ((hash1 << 5) + hash1) ^ value[i];
|
|
if (i == value.Length - 1)
|
|
break;
|
|
hash2 = ((hash2 << 5) + hash2) ^ value[i + 1];
|
|
}
|
|
result = hash1 + (hash2 * 1566083941);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} |