Skip to content

Add ToBase64String method for URL-safe Base64 encoding and parsing for Id#21

Merged
aevitas merged 2 commits intoaevitas:masterfrom
npenza:add-ToBase64String-and-deprecate-ToStringIdentifier
Sep 6, 2025
Merged

Add ToBase64String method for URL-safe Base64 encoding and parsing for Id#21
aevitas merged 2 commits intoaevitas:masterfrom
npenza:add-ToBase64String-and-deprecate-ToStringIdentifier

Conversation

@npenza
Copy link
Contributor

@npenza npenza commented Sep 4, 2025

Resolves #20.

This PR introduces a new and efficient way to serialize Ids as URL-safe Base64 strings. It includes parsing methods that support both the new format and the legacy ToStringIdentifier() Base64 string format, along with unit tests.

Changes included:

  • Added IdExtensions.ToBase64String() for URL-safe Base64 encoding of 64-bit IDs.
  • Replaces + and / with - and _ for URL safety.
  • Removes padding characters (=).
  • Added IdExtensions.FromBase64String() to parse both new URL-safe and legacy Base64 strings.
  • Added TryParseFromUrlSafeBase64() private helper for safe parsing of the new Base64 format.
  • Added TryParseFromLegacyBase64() private helper to maintain compatibility with ToStringIdentifier().
  • Updated ToStringIdentifier() to remain for legacy support and marked it as [Obsolete].

Added unit tests covering:

  • Round-trip encoding and decoding for both new and legacy formats.
  • URL safety and absence of padding in new format.
  • Handling invalid inputs.
  • Validation of short output versus legacy format.
  • Parsing specific edge-case ID values (0, -1, large values).
  • Verification that ToStringIdentifier is correctly marked [Obsolete].

Please let me know what you think or if any changes are needed.

…g parsing

- Introduces efficient, URL-safe Base64 encoding for 64-bit IDs
- Handles legacy ToStringIdentifier format for backward compatibility
- Includes TryParseFromUrlSafeBase64 and TryParseFromLegacyBase64 helpers
Copy link
Owner

@aevitas aevitas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very elaborate, nice job. Thanks for picking this one up!

@aevitas aevitas merged commit a61e585 into aevitas:master Sep 6, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ToStringIdentifier is inefficient

2 participants