From 6a4f47c31701e2d4206665b0f33f3f788a7a728a Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Wed, 26 Aug 2020 19:30:43 +0100 Subject: [PATCH 1/7] ft: add in constant to track min char set --- shortid/Utils/Constants.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shortid/Utils/Constants.cs b/shortid/Utils/Constants.cs index 617c3cc..5cfd198 100644 --- a/shortid/Utils/Constants.cs +++ b/shortid/Utils/Constants.cs @@ -5,5 +5,7 @@ internal static class Constants public const int MinimumAutoLength = 7; public const int MaximumAutoLength = 15; + + public const int MinimumCharacterSetLength = 20; } } From 344a433496808e6a937f547cb3708ccc74e4828b Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Wed, 26 Aug 2020 19:31:38 +0100 Subject: [PATCH 2/7] ft: update nuget info --- shortid/shortid.csproj | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/shortid/shortid.csproj b/shortid/shortid.csproj index 31ff0b2..923d48a 100644 --- a/shortid/shortid.csproj +++ b/shortid/shortid.csproj @@ -8,13 +8,16 @@ https://members.orcid.org/sites/default/files/vector_iD_icon.svg https://github.com/bolorundurowb/shortid/ shortid short id databse identifier key primarykey mongodb sql - - add in options validation + - fix- random usages Bolorunduro Winner-Timothy A library that generates random identifiers from 7 to 14 characters. Identifiers generated can be used as primary keys for databases or unique identifiers. true true shortid git - 2.0.3 + 2.0.4 + 2.0.4.0 + 2.0.4.0 + en-NG From 5c3668326ea35d35f892a86c5345a17f21df0fa7 Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Wed, 26 Aug 2020 19:32:35 +0100 Subject: [PATCH 3/7] ft: update char set to fix issues with zero and teh char O looking similar --- shortid/ShortId.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shortid/ShortId.cs b/shortid/ShortId.cs index 3863374..c26db68 100644 --- a/shortid/ShortId.cs +++ b/shortid/ShortId.cs @@ -8,8 +8,8 @@ public static class ShortId { // app variables private static Random _random = new Random(); - private const string Bigs = "ABCDEFGHIJKLMNOPQRSTUVWXY"; - private const string Smalls = "abcdefghjlkmnopqrstuvwxyz"; + private const string Bigs = "ABCDEFGHIJKLMNPQRSTUVWXY"; + private const string Smalls = "abcdefghjklmnopqrstuvwxyz"; private const string Numbers = "0123456789"; private const string Specials = "-_"; private static string _pool = $"{Smalls}{Bigs}"; From 9000c4c79dbeec65e59aa64127e8a766481fbc4c Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Wed, 26 Aug 2020 19:34:42 +0100 Subject: [PATCH 4/7] ft: switch length checks to be against constants --- shortid/ShortId.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/shortid/ShortId.cs b/shortid/ShortId.cs index c26db68..1ff1a2e 100644 --- a/shortid/ShortId.cs +++ b/shortid/ShortId.cs @@ -1,6 +1,7 @@ using System; using System.Text; using shortid.Configuration; +using shortid.Utils; namespace shortid { @@ -11,7 +12,7 @@ public static class ShortId private const string Bigs = "ABCDEFGHIJKLMNPQRSTUVWXY"; private const string Smalls = "abcdefghjklmnopqrstuvwxyz"; private const string Numbers = "0123456789"; - private const string Specials = "-_"; + private const string Specials = "_-"; private static string _pool = $"{Smalls}{Bigs}"; // thread management variables @@ -85,10 +86,10 @@ public static string Generate(GenerationOptions options) throw new ArgumentNullException(nameof(options)); } - if (options.Length < 7) + if (options.Length < Constants.MinimumAutoLength) { throw new ArgumentException( - $"The specified length of {options.Length} is less than the lower limit of 7."); + $"The specified length of {options.Length} is less than the lower limit of {Constants.MinimumAutoLength}."); } string characterPool; @@ -145,7 +146,7 @@ public static void SetCharacters(string characters) } } - if (stringBuilder.Length < 20) + if (stringBuilder.Length < Constants.MinimumCharacterSetLength) { throw new InvalidOperationException( "The replacement characters must be at least 20 letters in length and without whitespace."); From dd0a67966c562aeaaa21b63e21425d474c5083b7 Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Wed, 26 Aug 2020 19:35:54 +0100 Subject: [PATCH 5/7] ft: fix issues with random thread-safe access --- shortid/ShortId.cs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/shortid/ShortId.cs b/shortid/ShortId.cs index 1ff1a2e..3a413d7 100644 --- a/shortid/ShortId.cs +++ b/shortid/ShortId.cs @@ -92,15 +92,7 @@ public static string Generate(GenerationOptions options) $"The specified length of {options.Length} is less than the lower limit of {Constants.MinimumAutoLength}."); } - string characterPool; - Random rand; - - lock (ThreadLock) - { - characterPool = _pool; - rand = _random; - } - + var characterPool = _pool; var poolBuilder = new StringBuilder(characterPool); if (options.UseNumbers) { @@ -117,8 +109,11 @@ public static string Generate(GenerationOptions options) var output = new char[options.Length]; for (var i = 0; i < options.Length; i++) { - var charIndex = rand.Next(0, pool.Length); - output[i] = pool[charIndex]; + lock (ThreadLock) + { + var charIndex = _random.Next(0, pool.Length); + output[i] = pool[charIndex]; + } } return new string(output); From d71ff7ea97dc903227a62a4efed3d1d922ccf4b2 Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Wed, 26 Aug 2020 19:39:27 +0100 Subject: [PATCH 6/7] ft: update docs --- README.md | 60 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 77715ed..09c3dfa 100644 --- a/README.md +++ b/README.md @@ -4,20 +4,35 @@ ## About ShortId -A CSharp library to generate completely random short id's. They can be used as primary keys or unique identifiers. This library is different in that you can specify the length of the id's generated. I have tested the application generating 180000 id's without duplicates. +A CSharp library to generate completely random short id's. They can be used as primary keys or unique identifiers. This library is different in that you can specify the length of the ids to be generated. This library is thread-safe and can generate millions of unique ids across multiple threads. -## How to use +## Getting Started To make use of the `shortid`, add it to your project via the Nuget package manager UI or console via this command: +#### Package Manager + ``` Install-Package shortid ``` +#### .NET CLI +``` +> dotnet add package shortid +``` + +#### PackageReference +```csharp + +``` + +## Usage + Add the following using command to the top of your csharp code file: ```csharp using shortid; +using shortid.Configuration; ``` This gives your code access the classes and methods of the `shortid` namespace. @@ -29,36 +44,39 @@ string id = ShortId.Generate(); // id = KXTR_VzGVUoOY ``` -If you want to include numbers in the generated id, then you call the `Generate` method with a boolean indicating your preference. +If you want to include numbers in the generated id, then you call the `Generate` method with options indicating your preference. ```csharp -string id = ShortId.Generate(true); +var options = new GenerationOptions +{ + UseNumbers = true +}; +string id = ShortId.Generate(options); // id = O_bBY-YUkJg ``` -If you do not want special characters *i.e _ and -* in your generated id, then call the `Generate` method with two boolean parameters, the first indicating whether or not you want numbers and the second indicating whether or not you want special characeters. +If you do not want special characters *i.e _ and -* in your generated id, then call the `Generate` method with options indicating your preferences. ```csharp -string id = ShortId.Generate(true, false); +var options = new GenerationOptions +{ + UseSpecialCharacters = false +}; +string id = ShortId.Generate(options); // id = waBfk3z ``` -If you want to specify the length of the generated id, call the `Generate` method with an integer parameter which is the desired length. +If you want to specify the length of the generated id, call the `Generate` method with options indicating your preferences. ```csharp -string id = ShortId.Generate(8); -// id = M-snXzBk +var options = new GenerationOptions +{ + Length = 9 +}; +string id = ShortId.Generate(options); +// id = M-snXzBkj ``` -If you want to control the type of id generated by specifying whether you want numbers, special characters and the length, call the `Generate` method and pass three parameters, the first a boolean stating whether you want numbers, the second a boolean stating whether you want special characters, the last a number indicating your length preference. - -```csharp -string id = ShortId.Generate(true, false, 12); -// id = VvoCDPazES_w -``` - -**NOTE: v2.0.0 introduced a change that prevents lengths of less than 7** - ## Customize ShortId @@ -67,11 +85,11 @@ string id = ShortId.Generate(true, false, 12); To change the character set in use, run the following: ```csharp -string characters = //whatever you want; +string characters = "ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫"; //whatever you want; ShortId.SetCharacters(characters); ``` -**NOTE: the new character set must not be `null`, an empty string or whitespace. Also, all whitespace characters would be removed, finally the character set cannot be less than 20 characters.** +**NOTE: the new character set must not be `null`, an empty string or whitespace. Also, all whitespace and duplicate characters would be removed, finally the character set cannot be less than 20 characters.** `ShortId` also allows the seed for the random number generator to be set. @@ -86,4 +104,4 @@ Finally, `ShortId` allows for all customizations to be reset using the following ```csharp ShortId.Reset(); -``` +``` \ No newline at end of file From 6d206a4b93b8f30ef258cb99f332df5e48605ccc Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Wed, 26 Aug 2020 19:44:05 +0100 Subject: [PATCH 7/7] ft: updat etests --- shortid.Test/Configuration/GenerationOptions.Tests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shortid.Test/Configuration/GenerationOptions.Tests.cs b/shortid.Test/Configuration/GenerationOptions.Tests.cs index f339a5e..4724435 100644 --- a/shortid.Test/Configuration/GenerationOptions.Tests.cs +++ b/shortid.Test/Configuration/GenerationOptions.Tests.cs @@ -33,7 +33,7 @@ public void ShouldAssignRandomLengthOnInstantiation() options .Length .Should() - .BeGreaterThan(7); + .BeGreaterThan(6); options .Length .Should()