Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A base62 ID generator based on a counter
- public class CounterID
- {
- private const char _lastNum = '9', _lastAlphaLower = 'z', _lastAlphaUpper = 'Z'; // constants
- private readonly char[] _counter = "0000000000".ToCharArray(); // ten character starting value
- public void Increment() => Increment(9); // public method which doesnt expose any parameters
- private void Increment(int position) // private method for internal use
- {
- if (position == -1) return; // enables wrap around ex: ZZZZZZZZZZ increments to 0000000000
- switch (_counter[position])
- {
- case _lastNum:
- _counter[position] = 'a';
- break;
- case _lastAlphaLower:
- _counter[position] = 'A';
- break;
- case _lastAlphaUpper:
- _counter[position] = '0';
- Increment(position - 1);
- break;
- default:
- _counter[position]++;
- break;
- }
- }
- public override string ToString() => new(_counter); // return the char[] as a string
- }
Add Comment
Please, Sign In to add comment