TizzyT

CounterID -TizzyT

Jun 26th, 2021 (edited)
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. // A base62 ID generator based on a counter
  2.  
  3. public class CounterID
  4. {        
  5.     private const char _lastNum = '9', _lastAlphaLower = 'z', _lastAlphaUpper = 'Z'; // constants
  6.  
  7.     private readonly char[] _counter = "0000000000".ToCharArray(); // ten character starting value
  8.  
  9.     public void Increment() => Increment(9); // public method which doesnt expose any parameters
  10.     private void Increment(int position) // private method for internal use
  11.     {
  12.         if (position == -1) return; // enables wrap around ex: ZZZZZZZZZZ increments to 0000000000              
  13.         switch (_counter[position])
  14.         {
  15.             case _lastNum:
  16.                 _counter[position] = 'a';
  17.                 break;
  18.             case _lastAlphaLower:
  19.                 _counter[position] = 'A';
  20.                 break;
  21.             case _lastAlphaUpper:
  22.                 _counter[position] = '0';
  23.                 Increment(position - 1);
  24.                 break;
  25.             default:
  26.                 _counter[position]++;
  27.                 break;
  28.         }
  29.     }
  30.  
  31.     public override string ToString() => new(_counter); // return the char[] as a string
  32. }
Add Comment
Please, Sign In to add comment