Skip to content

Commit

Permalink
Some very minor gramatical and code fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
braillescreen committed Sep 13, 2024
1 parent fa13ae0 commit 50f4d95
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# token_gen_flag
This enum holds various constants that can be passed to the mode parameter in order to change how tokens are generated in `generate_token` function.
This enum holds various constants that can be passed to the mode parameter in order to change how tokens are generated in the `generate_token` function.

* token_gen_flag_all: Uses all characters, numbers and symbols, see below.
* token_gen_flag_characters: Uses only characters, a-z, A-Z
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
Generates a string of random characters, or token.
Generates a random string of characters (a token).
string generate_token(int token_length, int mode = token_gen_flag_all)
## Arguments:
* int token_length: the length of the token to generate.
* int mode = token_gen_flag_all: the mode to generate.
* int mode = token_gen_flag_all: allows you to specify which characters you would like in the generated token (see remarks).
## returns:
String: a random token depending on the mode.
## Remarks:
This function uses `generate_custom_token` function to generate. The characters used to generate the token will depend on the mode you set. See `token_gen_flags` enum constants.
This function uses the `generate_custom_token` function to generate. The characters used to generate the token will depend on the mode you specified. See `token_gen_flags` enum constants.
*/

// Example:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
Generates a string of random characters, or token base on the characters you set.
string generate_custom_token(int token_length, characters);
Generates a random string of characters (a token) while allowing you to directly specify the characters you wish to use in the generation.
string generate_custom_token(int token_length, string characters);
## Arguments:
* int token_length: the length of the token to generate.
* string characters: a list of characters to generate.
## returns:
String: a random token.
## Remarks:
If characters list is empty or token length is set to 0 or less, an empty string is returned.
If the `characters` string is empty or `token_length` is set to 0 or less, an empty string is returned.
*/

// Example:
Expand Down
9 changes: 6 additions & 3 deletions release/include/token_gen.nvgt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
//Possible token mode.

//Possible token modes.
enum token_gen_flag {
token_gen_flag_all = 0,
token_gen_flag_characters,
Expand All @@ -18,22 +19,24 @@ enum token_gen_flag {
token_gen_flag_numbers_symbols,
token_gen_flag_characters_symbols
}

string generate_token(int token_length, int mode = token_gen_flag_all) {
string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string numbers = "1234567890";
string symbols = "`~!@#$%^&*()_+=-[]{}/.,;:|?><";
string token_sims = chars + numbers + symbols;
string token_sims;
if (mode == token_gen_flag_characters) token_sims = chars;
else if (mode == token_gen_flag_numbers) token_sims = numbers;
else if (mode == token_gen_flag_symbols) token_sims = symbols;
else if (mode == token_gen_flag_numbers_symbols) token_sims = numbers + symbols;
else if (mode == token_gen_flag_characters_symbols) token_sims = chars + symbols;
return generate_custom_token(token_length, token_sims);
}

string generate_custom_token(int token_length, string characters) {
if (characters.empty() || token_length <= 0) return "";
string final_token;
for (uint i = 0; i < token_length; i++)
final_token += characters[random(0, characters.length() - 1)];
return final_token;
return final_token;
}

2 comments on commit 50f4d95

@harrymkt
Copy link
Contributor

Choose a reason for hiding this comment

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

Hello @braillescreen.
Removing the string token_sims = chars + numbers + symbols; in token_gen.nvgt will cause the token gen flag enums to stop functioning, especially for token_gen_flag_all flag, because the code has been made so that if the number reaches its supported value, its mode will fall back to all.

@braillescreen
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks much and sorry about that; it's all fixed now!

Please sign in to comment.