Skip to content

Commit cd0e4dc

Browse files
committed
Update consolidated snippets
1 parent bf35e2e commit cd0e4dc

File tree

10 files changed

+272
-911
lines changed

10 files changed

+272
-911
lines changed

public/consolidated/c.json

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
"description": "Prints Hello, World! to the terminal.",
88
"author": "0xHouss",
99
"tags": [
10-
"c",
1110
"printing",
12-
"hello-world",
13-
"utility"
11+
"hello-world"
1412
],
1513
"contributors": [],
1614
"code": "#include <stdio.h> // Includes the input/output library\n\nint main() { // Defines the main function\n printf(\"Hello, World!\\n\") // Outputs Hello, World! and a newline\n\n return 0; // indicate the program executed successfully\n}\n"
@@ -25,26 +23,11 @@
2523
"description": "Calculates the factorial of a number.",
2624
"author": "0xHouss",
2725
"tags": [
28-
"c",
2926
"math",
30-
"factorial",
31-
"utility"
27+
"factorial"
3228
],
3329
"contributors": [],
34-
"code": "int factorial(int x) {\n int y = 1;\n\n for (int i = 2; i <= x; i++)\n y *= i;\n\n return y;\n}\n"
35-
},
36-
{
37-
"title": "Power Function",
38-
"description": "Calculates the power of a number.",
39-
"author": "0xHouss",
40-
"tags": [
41-
"c",
42-
"math",
43-
"power",
44-
"utility"
45-
],
46-
"contributors": [],
47-
"code": "int power(int x, int n) {\n int y = 1;\n\n for (int i = 0; i < n; i++)\n y *= x;\n\n return y;\n}\n"
30+
"code": "int factorial(int x) {\n int y = 1;\n\n for (int i = 2; i <= x; i++)\n y *= i;\n\n return y;\n}\n\n// Usage:\nfactorial(4); // Returns: 24\n"
4831
}
4932
]
5033
}

public/consolidated/cpp.json

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
"description": "Prints Hello, World! to the terminal.",
88
"author": "James-Beans",
99
"tags": [
10-
"cpp",
1110
"printing",
12-
"hello-world",
13-
"utility"
11+
"hello-world"
1412
],
1513
"contributors": [],
1614
"code": "#include <iostream> // Includes the input/output stream library\n\nint main() { // Defines the main function\n std::cout << \"Hello, World!\" << std::endl; // Outputs Hello, World! and a newline\n return 0; // indicate the program executed successfully\n}\n"
@@ -25,13 +23,12 @@
2523
"description": "Convert vector into queue quickly",
2624
"author": "mrityunjay2003",
2725
"tags": [
28-
"cpp",
2926
"data structures",
3027
"queue",
3128
"vector"
3229
],
3330
"contributors": [],
34-
"code": "#include<queue>\n#include<vector>\n#include<deque>\n\nstd::queue<int> vectorToQueue(const std::vector<int>& v) {\n return std::queue<int>(std::deque<int>(v.begin(), v.end()));\n}\n"
31+
"code": "#include<queue>\n#include<vector>\n#include<deque>\n\nstd::queue<int> vectorToQueue(const std::vector<int>& v) {\n return std::queue<int>(std::deque<int>(v.begin(), v.end()));\n}\n\nstd::vector<int> vec = { 1, 2, 3, 4, 5 };\nvectorToQueue(&vec); // Returns: std::queue<int> { 1, 2, 3, 4, 5 }\n"
3532
}
3633
]
3734
},
@@ -43,12 +40,11 @@
4340
"description": "Check if an integer is a prime number",
4441
"author": "MihneaMoso",
4542
"tags": [
46-
"cpp",
4743
"number",
4844
"prime"
4945
],
5046
"contributors": [],
51-
"code": "bool is_prime(int n) {\n if (n < 2) return false;\n if (n == 2 || n == 3) return true;\n if (n % 2 == 0) return false;\n for (int i = 3; i * i <= n; i += 2) {\n if (n % i == 0) return false;\n }\n return true;\n}\n\n// Usage\n#include <iostream>\n\nint main() {\n std::cout << is_prime(29) << std::endl; // Output: 1\n return 0;\n}\n"
47+
"code": "bool is_prime(int n) {\n if (n < 2) return false;\n if (n == 2 || n == 3) return true;\n if (n % 2 == 0) return false;\n for (int i = 3; i * i <= n; i += 2) {\n if (n % i == 0) return false;\n }\n return true;\n}\n\n// Usage:\nis_prime(29); // Returns: true\n"
5248
}
5349
]
5450
},
@@ -60,26 +56,22 @@
6056
"description": "Reverses the characters in a string.",
6157
"author": "Vaibhav-kesarwani",
6258
"tags": [
63-
"cpp",
6459
"array",
65-
"reverse",
66-
"utility"
60+
"reverse"
6761
],
6862
"contributors": [],
69-
"code": "#include <string>\n#include <algorithm>\n\nstd::string reverseString(const std::string& input) {\n std::string reversed = input;\n std::reverse(reversed.begin(), reversed.end());\n return reversed;\n}\n"
63+
"code": "#include <string>\n#include <algorithm>\n\nstd::string reverseString(const std::string& input) {\n std::string reversed = input;\n std::reverse(reversed.begin(), reversed.end());\n return reversed;\n}\n\nreverseString(\"quicksnip\"); // Returns: \"pinskciuq\"\n"
7064
},
7165
{
7266
"title": "Split String",
7367
"description": "Splits a string by a delimiter",
7468
"author": "saminjay",
7569
"tags": [
76-
"cpp",
7770
"string",
78-
"split",
79-
"utility"
71+
"split"
8072
],
8173
"contributors": [],
82-
"code": "#include <string>\n#include <vector>\n\nstd::vector<std::string> split_string(std::string str, std::string delim) {\n std::vector<std::string> splits;\n int i = 0, j;\n int inc = delim.length();\n while (j != std::string::npos) {\n j = str.find(delim, i);\n splits.push_back(str.substr(i, j - i));\n i = j + inc;\n }\n return splits;\n}\n"
74+
"code": "#include <string>\n#include <vector>\n\nstd::vector<std::string> split_string(std::string str, std::string delim) {\n std::vector<std::string> splits;\n int i = 0, j;\n int inc = delim.length();\n while (j != std::string::npos) {\n j = str.find(delim, i);\n splits.push_back(str.substr(i, j - i));\n i = j + inc;\n }\n return splits;\n}\n\n// Usage:\nsplit_string(\"quick_-snip\", \"_-\"); // Returns: std::vector<std::string> { \"quick\", \"snip\" }\n"
8375
}
8476
]
8577
}

public/consolidated/csharp.json

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
"description": "Prints Hello, World! to the terminal.",
88
"author": "chaitanya-jvnm",
99
"tags": [
10-
"c#",
1110
"printing",
12-
"hello-world",
13-
"utility"
11+
"hello-world"
1412
],
1513
"contributors": [],
1614
"code": "public class Program {\n public static void Main(string[] args) {\n System.Console.WriteLine(\"Hello, World!\");\n }\n}\n"
@@ -25,26 +23,22 @@
2523
"description": "Generates a new GUID",
2624
"author": "chaitanya-jvnm",
2725
"tags": [
28-
"c#",
2926
"guid",
30-
"generate",
31-
"utility"
27+
"generate"
3228
],
3329
"contributors": [],
34-
"code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n"
30+
"code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n\n// Usage:\nGenerateGuid(); // Returns: 1c4c38d8-64e4-431b-884a-c6eec2ab02cd (Uuid is random)\n"
3531
},
3632
{
3733
"title": "Validate GUID",
3834
"description": "Checks if a string is a valid GUID.",
3935
"author": "chaitanya-jvnm",
4036
"tags": [
41-
"c#",
4237
"guid",
43-
"validate",
44-
"utility"
38+
"validate"
4539
],
4640
"contributors": [],
47-
"code": "public static bool IsGuid(string str) {\n return Guid.TryParse(str, out _);\n}\n"
41+
"code": "public static bool IsGuid(string str) {\n return Guid.TryParse(str, out _);\n}\n\n// Usage:\nIsGuid(\"1c4c38d8-64e4-431b-884a-c6eec2ab02cd\"); // Returns: true\nIsGuid(\"quicksnip\"); // Returns: false\n"
4842
}
4943
]
5044
},
@@ -56,57 +50,38 @@
5650
"description": "Decodes a JWT.",
5751
"author": "chaitanya-jvnm",
5852
"tags": [
59-
"c#",
6053
"jwt",
61-
"decode",
62-
"utility"
54+
"decode"
6355
],
6456
"contributors": [],
65-
"code": "/// <summary>\n/// Decodes the JWT\n/// <summary>\npublic static string DecodeJwt(string token) {\n return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();\n}\n\n//Example\nstring token = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\n\nstring decodedJwt = DecodeJwt(token);\n\nConsole.WriteLine(decodedJwt); //Prints {\"alg\":\"HS256\",\"typ\":\"JWT\"}.{\"sub\":\"1234567890\",\"name\":\"John Doe\",\"iat\":1516239022}\n"
66-
},
67-
{
68-
"title": "Generate JWT",
69-
"description": "Generates a new JWT.",
70-
"author": "chaitanya-jvnm",
71-
"tags": [
72-
"c#",
73-
"jwt",
74-
"generate",
75-
"utility"
76-
],
77-
"contributors": [],
78-
"code": "public static string GenerateJwt(string secret, string issuer, string audience, int expirationMinutes) {\n var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret));\n var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);\n var token = new JwtSecurityToken(issuer, audience, null, expires: DateTime.UtcNow.AddMinutes(expirationMinutes), signingCredentials: credentials);\n return new JwtSecurityTokenHandler().WriteToken(token);\n}\n"
57+
"code": "public static string DecodeJwt(string token) {\n return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();\n}\n\n// Usage:\nstring token = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\nDecodeJwt(token); // Returns: \"{\\\"alg\\\":\\\"HS256\\\",\\\"typ\\\":\\\"JWT\\\"}.{\\\"sub\\\":\\\"1234567890\\\",\\\"name\\\":\\\"John Doe\\\",\\\"iat\\\":1516239022}\"\n"
7958
},
8059
{
8160
"title": "Validate JWT",
8261
"description": "Validates a JWT.",
8362
"author": "chaitanya-jvnm",
8463
"tags": [
85-
"c#",
8664
"jwt",
87-
"validate",
88-
"utility"
65+
"validate"
8966
],
9067
"contributors": [],
91-
"code": "public static bool ValidateJwt(string token, string secret) {\n var tokenHandler = new JwtSecurityTokenHandler();\n var validationParameters = new TokenValidationParameters {\n ValidateIssuerSigningKey = true,\n IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),\n ValidateIssuer = false,\n ValidateAudience = false\n };\n try {\n tokenHandler.ValidateToken(token, validationParameters, out _);\n return true;\n }\n catch {\n return false\n }\n}\n\n//Example\nstring JWT = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\n\nstring correctSecret = \"your-256-bit-secret\";\nstring wrongSecret = \"this-is-not-the-right-secret\";\n\nConsole.WriteLine(ValidateJwt(JWT, correctSecret)) // returns True\nConsole.WriteLine(ValidateJwt(JWT, wrongSecret)) // returns False\n\n"
68+
"code": "public static bool ValidateJwt(string token, string secret) {\n var tokenHandler = new JwtSecurityTokenHandler();\n var validationParameters = new TokenValidationParameters {\n ValidateIssuerSigningKey = true,\n IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),\n ValidateIssuer = false,\n ValidateAudience = false\n };\n try {\n tokenHandler.ValidateToken(token, validationParameters, out _);\n return true;\n }\n catch {\n return false\n }\n}\n\n// Usage:\nstring JWT = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\nstring correctSecret = \"your-256-bit-secret\";\nstring wrongSecret = \"this-is-not-the-right-secret\";\n\nValidateJwt(JWT, correctSecret); // Returns: true\nValidateJwt(JWT, wrongSecret); // Returns: false\n"
9269
}
9370
]
9471
},
9572
{
9673
"categoryName": "List Utilities",
9774
"snippets": [
9875
{
99-
"title": "Swap two items at determined indexes",
76+
"title": "Swap items at index",
10077
"description": "Swaps two items at determined indexes",
10178
"author": "omegaleo",
10279
"tags": [
103-
"csharp",
104-
"c#",
10580
"list",
106-
"utility"
81+
"swapping"
10782
],
10883
"contributors": [],
109-
"code": "/// <summary>\n/// Swaps the position of 2 elements inside of a List\n/// </summary>\n/// <returns>List with swapped elements</returns>\npublic static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB)\n{\n (list[indexA], list[indexB]) = (list[indexB], list[indexA]);\n return list;\n}\n\nvar list = new List<string>() {\"Test\", \"Test2\"};\n\nConsole.WriteLine(list[0]); // Outputs: Test\nConsole.WriteLine(list[1]); // Outputs: Test2\n\nlist = list.Swap(0, 1).ToList();\n\nConsole.WriteLine(list[0]); // Outputs: Test2\nConsole.WriteLine(list[1]); // Outputs: Test\n"
84+
"code": "public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB)\n{\n (list[indexA], list[indexB]) = (list[indexB], list[indexA]);\n return list;\n}\n\nvar list = new List<string>() {\"Test\", \"Test2\"};\n\nlist.Swap(0, 1); // Swaps \"Test\" and \"Test2\" in place\n"
11085
}
11186
]
11287
},
@@ -118,26 +93,22 @@
11893
"description": "Makes the first letter of a string uppercase.",
11994
"author": "chaitanya-jvnm",
12095
"tags": [
121-
"c#",
12296
"string",
123-
"capitalize",
124-
"utility"
97+
"capitalize"
12598
],
12699
"contributors": [],
127-
"code": "/// <summary>\n/// Capitalize the first character of the string\n/// <summary>\npublic static string Capitalize(this string str) {\n return str.Substring(0, 1).ToUpper() + str.Substring(1);\n}\n\n//Example\nstring example = \"hello\";\nstring captializedExample = example.Capitalize();\nConsole.WriteLine(captializedExample); // prints \"Hello\"\n"
100+
"code": "public static string Capitalize(this string str) {\n return str.Substring(0, 1).ToUpper() + str.Substring(1);\n}\n\n// Usage:\n\"quicksnip\".Capitalize(); // Returns: \"Quicksnip\"\n"
128101
},
129102
{
130-
"title": "Truncate a String",
103+
"title": "Truncate String",
131104
"description": "Cut off a string once it reaches a determined amount of characters and add '...' to the end of the string",
132105
"author": "omegaleo",
133106
"tags": [
134-
"csharp",
135-
"c#",
136-
"list",
137-
"utility"
107+
"string",
108+
"truncate"
138109
],
139110
"contributors": [],
140-
"code": "/// <summary>\n/// Cut off a string once it reaches a <paramref name=\"maxChars\"/> amount of characters and add '...' to the end of the string\n/// </summary>\npublic static string Truncate(this string value, int maxChars)\n{\n return value.Length <= maxChars ? value : value.Substring(0, maxChars) + \"...\";\n}\n\nvar str = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tristique rhoncus bibendum. Vivamus laoreet tortor vel neque lacinia, nec rhoncus ligula pellentesque. Nullam eu ornare nibh. Donec tincidunt viverra nulla.\";\n\nConsole.WriteLine(str); // Outputs the full string\nConsole.WriteLine(str.Truncate(5)); // Outputs Lorem...\n"
111+
"code": "public static string Truncate(this string value, int maxChars)\n{\n return value.Length <= maxChars ? value : value.Substring(0, maxChars) + \"...\";\n}\n\n// Usage:\n\"Quicksnip\".Truncate(5); // Returns: \"Quick...\"\n"
141112
}
142113
]
143114
}

0 commit comments

Comments
 (0)