96 lines
3.8 KiB
C#
96 lines
3.8 KiB
C#
using View_by_Distance.Shared.Models.Properties;
|
|
using View_by_Distance.Shared.Models.Stateless.Methods;
|
|
|
|
namespace View_by_Distance.Shared.Models.Stateless;
|
|
|
|
internal abstract class Id
|
|
{
|
|
|
|
internal static bool NameWithoutExtensionIsIdFormat(string fileNameWithoutExtension)
|
|
{
|
|
bool result;
|
|
int intMinValueLength = int.MinValue.ToString().Length;
|
|
if (fileNameWithoutExtension.Length < 5 || fileNameWithoutExtension.Length > intMinValueLength)
|
|
result = false;
|
|
else
|
|
{
|
|
bool skipOneAllAreNumbers = fileNameWithoutExtension[1..].All(l => char.IsNumber(l));
|
|
result = (skipOneAllAreNumbers && fileNameWithoutExtension[0] == '-') || (skipOneAllAreNumbers && char.IsNumber(fileNameWithoutExtension[0]));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
internal static FilePath GetFilePath(FilePath filePath, string file)
|
|
{
|
|
FilePath result;
|
|
string fileName = Path.GetFileName(file);
|
|
string fileExtensionLowered = Path.GetExtension(file).ToLower();
|
|
result = new(filePath.DirectoryName, fileExtensionLowered, file, filePath.Id, filePath.IsIdFormat, filePath.IsPaddedIdFormat, fileName, filePath.NameWithoutExtension);
|
|
return result;
|
|
}
|
|
|
|
internal static FilePath GetFilePath(IMetadataConfiguration configuration, string file)
|
|
{
|
|
FilePath result;
|
|
int? id;
|
|
short? multiplier;
|
|
char negativeMarker;
|
|
int absoluteValueOfId;
|
|
string fileName = Path.GetFileName(file);
|
|
string fileExtensionLowered = Path.GetExtension(file).ToLower();
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
|
short sortOrderOnlyLengthIndex = IId.GetSortOrderOnlyLengthIndex(configuration.Offset);
|
|
string fileDirectoryName = Path.GetDirectoryName(file) ?? throw new NullReferenceException();
|
|
bool nameWithoutExtensionIsIdFormat = IId.NameWithoutExtensionIsIdFormat(fileNameWithoutExtension);
|
|
bool nameWithoutExtensionIsPaddedIdFormat = IId.NameWithoutExtensionIsPaddedIdFormat(fileNameWithoutExtension, sortOrderOnlyLengthIndex);
|
|
if (!nameWithoutExtensionIsIdFormat && !nameWithoutExtensionIsPaddedIdFormat)
|
|
id = null;
|
|
else if (nameWithoutExtensionIsIdFormat)
|
|
{
|
|
if (!int.TryParse(fileNameWithoutExtension, out absoluteValueOfId))
|
|
id = null;
|
|
else
|
|
id = absoluteValueOfId;
|
|
}
|
|
else
|
|
{
|
|
negativeMarker = fileNameWithoutExtension[sortOrderOnlyLengthIndex - 2];
|
|
if (negativeMarker == '7')
|
|
multiplier = 1;
|
|
else if (negativeMarker == '3')
|
|
multiplier = -1;
|
|
else
|
|
multiplier = null;
|
|
if (!int.TryParse(fileNameWithoutExtension[sortOrderOnlyLengthIndex..], out absoluteValueOfId))
|
|
id = null;
|
|
else
|
|
{
|
|
id = absoluteValueOfId * multiplier;
|
|
if (id is null || !fileNameWithoutExtension.EndsWith(id.Value.ToString()[1..]))
|
|
id = null;
|
|
}
|
|
}
|
|
result = new(fileDirectoryName, fileExtensionLowered, file, id, nameWithoutExtensionIsIdFormat, nameWithoutExtensionIsPaddedIdFormat, fileName, fileNameWithoutExtension);
|
|
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;
|
|
}
|
|
|
|
} |