Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imprive public id parsing #61

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Misc/Curiosity.Tools/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [1.5.2]

### Changed

- `PublicId.ToPublicId()` returns 17 chars string instead 16 chars
- `PublicId.TryParse()` can parse hex and dec line

## [1.5.1] - 2023-11-28

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/Misc/Curiosity.Tools/Curiosity.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<PackageVersion>1.5.1</PackageVersion>
<PackageVersion>1.5.2</PackageVersion>

<Authors>Max Markelow (@markeli), Andrey Ioch (@DevCorvette)</Authors>
<Company>SIIS Ltd</Company>
Expand Down
39 changes: 35 additions & 4 deletions src/Misc/Curiosity.Tools/UniqueId/PublicId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,51 @@ public static class PublicId
/// <returns>ID in HEX</returns>
public static string ToPublicId(this long id)
{
return id.ToString("x16", CultureInfo.InvariantCulture);
// number 17 in the format indicates the minimum number of characters in the string
// missing characters are replaced with 0
// by the first 0, we can detect that the number is hexadecimal, even if all numbers are there
// until 2024 year, our UniqueIdGenerator had generated 16 characters string
return id.ToString("x17", CultureInfo.InvariantCulture);
}

/// <summary>
/// Converts an ID from a "public" view to a regular long.
/// </summary>
/// <param name= "exportId" >Public version of the ID (only in hex format)</param>
/// <param name= "exportId" >String number can be hex or dec</param>
/// <param name= "id" >Our regular ID</param>
/// <returns>Successfully parted?</returns>
public static bool TryParse(string exportId, out long id)
{
id = 0;
return !String.IsNullOrWhiteSpace(exportId) &&
long.TryParse(exportId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out id);
if (String.IsNullOrWhiteSpace(exportId))
return false;

// try parse as hex or dec
return IsHexFormat(exportId)
? long.TryParse(exportId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out id)
: long.TryParse(exportId, out id);
}

/// <summary>
/// Detects that line format is hex or dec
/// </summary>
private static bool IsHexFormat(string line)
{
// hex when contains non numeric symbols
for (var i = 0; i < line.Length; i++)
{
if (!char.IsDigit(line[i]))
return true;
}

// until 2024 year, our UniqueIdGenerator had generated 16 chars string then 17 chars
// always with first 0
if ((line.Length == 16 || line.Length == 17) &&
line[0] == '0')
return true;

// exactly dec
return false;
}
}
}
2 changes: 2 additions & 0 deletions src/Misc/Curiosity.Tools/UniqueId/UniqueIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public static class UniqueIdGenerator
private const int MaxSequenceId = 4096;

// 13 apr 2020 (in Ticks / 10000)
// can generate positive long numbers ~70 years from this date,
// then a overflow starts
private const long ModelStartEpoch = 63722332800000;
private static long _generatorId = -1;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using FluentAssertions;
using Xunit;

namespace Curiosity.Tools.UnitTests.UniqueId
{
public class PublicIdTests
{
// can detect that line is 16 chars hex and parse it to true long value
[Fact]
public void TryParse_Parse16CharsHexLine_LongValue()
{
// arrange
const long value = 10752219502637056;
const string hex = "0026331630007000"; // 16 chars hex

// act
var canParse = PublicId.TryParse(hex, out var id);

// assert
canParse.Should().Be(true);
id.Should().Be(value);
}

// can detect that line is 17 chars hex and parse it to true long value
[Fact]
public void TryParse_Parse17CharsHexLine_LongValue()
{
// arrange
const long value = 10752219502637056;
const string hex = "00026331630007000"; // 17 chars hex

// act
var canParse = PublicId.TryParse(hex, out var id);

// assert
canParse.Should().Be(true);
id.Should().Be(value);
}

// can detect that line is dec and parse it to true long value
[Fact]
public void TryParse_ParseDecLine_LongValue()
{
// arrange
const long value = 10752219502637056;
const string dec = "10752219502637056"; // dec line

// act
var canParse = PublicId.TryParse(dec, out var id);

// assert
canParse.Should().Be(true);
id.Should().Be(value);
}
}
}
Loading