From 074b0c88075de4867090989ddd6b995402aaa76f Mon Sep 17 00:00:00 2001 From: shwetha729 Date: Fri, 1 Sep 2023 08:27:59 -0400 Subject: [PATCH] Updated: Friday, September 01,2023-09-01 08:04:15 --- .../plugins/recent-files-obsidian/data.json | 2 +- enter/.obsidian/workspace.json | 10 +- enter/C++.md | 6 - .../Terminal Tips/Languages/C++.md | 112 ++++++++++++++++++ 4 files changed, 118 insertions(+), 12 deletions(-) delete mode 100644 enter/C++.md create mode 100644 enter/Coding Tips (Classical)/Terminal Tips/Languages/C++.md diff --git a/enter/.obsidian/plugins/recent-files-obsidian/data.json b/enter/.obsidian/plugins/recent-files-obsidian/data.json index b84635e..de395d9 100644 --- a/enter/.obsidian/plugins/recent-files-obsidian/data.json +++ b/enter/.obsidian/plugins/recent-files-obsidian/data.json @@ -2,7 +2,7 @@ "recentFiles": [ { "basename": "C++", - "path": "C++.md" + "path": "Coding Tips (Classical)/Terminal Tips/Languages/C++.md" }, { "basename": "gdevelop.io", diff --git a/enter/.obsidian/workspace.json b/enter/.obsidian/workspace.json index 49967f4..628afdb 100644 --- a/enter/.obsidian/workspace.json +++ b/enter/.obsidian/workspace.json @@ -65,7 +65,7 @@ "state": { "type": "markdown", "state": { - "file": "C++.md", + "file": "Coding Tips (Classical)/Terminal Tips/Languages/C++.md", "mode": "source", "backlinks": false, "source": false @@ -148,7 +148,7 @@ "state": { "type": "outline", "state": { - "file": "C++.md" + "file": "Coding Tips (Classical)/Terminal Tips/Languages/C++.md" } } }, @@ -158,7 +158,7 @@ "state": { "type": "outgoing-link", "state": { - "file": "C++.md", + "file": "Coding Tips (Classical)/Terminal Tips/Languages/C++.md", "linksCollapsed": false, "unlinkedCollapsed": false } @@ -183,10 +183,10 @@ "audio-recorder:Start/stop recording": false } }, - "active": "a4aa933fc978113e", + "active": "28a54cc81af4607b", "lastOpenFiles": [ "Coding Tips (Classical)/Terminal Tips/GUIs/Games/gdevelop.io.md", - "C++.md", + "Coding Tips (Classical)/Terminal Tips/Languages/C++.md", "Coding Tips (Classical)/Terminal Tips/GUIs/Games/Neopets.md", "Coding Tips (Classical)/Terminal Tips/GUIs/Games/Ruffle.md", "Coding Tips (Classical)/Terminal Tips/GUIs/Tools/StackBlitz.md", diff --git a/enter/C++.md b/enter/C++.md deleted file mode 100644 index 5ef80aa..0000000 --- a/enter/C++.md +++ /dev/null @@ -1,6 +0,0 @@ -# C++ - -C++ is one of the most used languages out there for writing low-level code. There are various applications and projects based off of C++. - -- A URL shortener in C++ - - gist r \ No newline at end of file diff --git a/enter/Coding Tips (Classical)/Terminal Tips/Languages/C++.md b/enter/Coding Tips (Classical)/Terminal Tips/Languages/C++.md new file mode 100644 index 0000000..c4d88d7 --- /dev/null +++ b/enter/Coding Tips (Classical)/Terminal Tips/Languages/C++.md @@ -0,0 +1,112 @@ +# C++ + +C++ is one of the most used languages out there for writing low-level code. There are various applications and projects based off of C++. + +- A URL shortener in C++ + - [gist](https://gist.github.com/jaytaylor/a11fadf61a869ade0dfe568606b216c8#file-base64-url-shortener-poc-cpp) +```c++ +/** + * URL shortener Base-62 encoder / decoder C++ Proof-of-Concept. + * + * Created as a resource for https://stackoverflow.com/a/742047/293064. + * + * How to compile: + * + * g++ -o base64-url-shortener-poc base64-url-shortener-poc.cpp + * + * Usage: + * + * ./base64-url-shortener-poc [ID] + * + * Example output: + * + * Input ID: 99592 + * --- + * Generated short URL: z4u + * ID decoded from URL: 99592 + * + * @author Jay Taylor + * + * @date 2019-07-08 + * + */ + +#include +#include +#include +#include + +using namespace std; + +/** + * id_to_short_url takes an input ID and produces a short URL suffix. + * + * Base-62 encodes the identifier. + */ +string id_to_short_url(unsigned int n) { + // Mapping which defines the 62 possible output characters. + char map[] = "abcdefghijklmnopqrstuvwxyzABCDEF" + "GHIJKLMNOPQRSTUVWXYZ0123456789"; + + string short_url; + + // Convert given ID to a base-62 number. + while (n) { + // Append each character mapped by the remainder. + short_url.push_back(map[n % 62]); + n /= 62; + } + + // Reverse the string to complete the base conversion. + reverse(short_url.begin(), short_url.end()); + + return short_url; +} + +/** + * short_url_to_id converts a short URL into the corresponding ID. + * + * Base-62 decodes the input string. + */ +unsigned int short_url_to_id(string short_url) { + unsigned int id = 0; + + // Base decode conversion logic. + for (int i = 0; i < short_url.length(); ++i) { + if ('a' <= short_url[i] && short_url[i] <= 'z') { + id = id * 62 + short_url[i] - 'a'; + } + if ('A' <= short_url[i] && short_url[i] <= 'Z') { + id = id * 62 + short_url[i] - 'A' + 26; + } + if ('0' <= short_url[i] && short_url[i] <= '9') { + id = id * 62 + short_url[i] - '0' + 52; + } + } + return id; +} + +int main(int argc, char** argv) { + if (argc == 1) { + cerr << "error: missing required parameter: id" << endl; + return 1; + } + + unsigned int n = atoi(argv[1]); + + if (n <= 0) { + cerr << "error: invalid input value, an integer greater than 0 is required" << endl; + return 1; + } + + cout << "Input ID: " << n << endl << "---" << endl; + + string encoded_short_url = id_to_short_url(n); + unsigned int decoded_id = short_url_to_id(encoded_short_url); + + cout << "Generated short URL: " << encoded_short_url << endl; + cout << "ID decoded from URL: " << decoded_id << endl; + return 0; +} +``` +