summaryrefslogtreecommitdiffstats
path: root/main.cpp
blob: 4752d6ca4a5ab30febe509ab50a646ca43840037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <iostream>
#include <StringTools/Stringtools.h>
#include <string>
#include <sstream>
#include <functional>
#include <algorithm>
#include <random>

//! Generic uppercase character.
constexpr char UPPERCASE = 0;

//! Generic lowercase character.
constexpr char LOWERCASE = (1<<5);

//! Checks whether or not `c` is a vowel. You can define custom vowel characters.
bool IsVowel(const char c, const std::string& vowels = "euioay") {
    return std::any_of(
            vowels.cbegin(),
            vowels.cend(),
            [c](const char vowel) {
                    return c == vowel;
                }
        );
}

//! Returns whether or not `c` is a letter.
bool IsLetter(const char c) {
    const char lowercase_c = !(c & (1<<5)) ? (c | (1<<5)) : c; // Re-implementing IsUpper and MakeLower to prevent stack-overflow by endless recursion
    return (lowercase_c >= 'a') && (lowercase_c <= 'z');
}

//! Returns whether or not `c` is a generic character (that contains JUST the sign)
bool IsGeneric(const char c) {
    return (c == UPPERCASE) || (c == LOWERCASE);
}

//! Checks whether or not `c` is an uppercase character.
bool IsUpper(const char c) {
    if ((!IsLetter(c)) && (!IsGeneric(c)))
        return false;
    else
        return !(c & (1<<5));
}

//! Will return `c` as an uppercase character.
char MakeUpper(char c) {
    if ((!IsLetter(c)) && (!IsGeneric(c)))
        return c;
    else if (IsUpper(c))
        return c;
    else
        return c & ~(1<<5);
}

//! Will return `c` as a lowercase character.
char MakeLower(char c) {
    if ((!IsLetter(c)) && (!IsGeneric(c)))
        return c;
    else if (!IsUpper(c))
        return c;
    else
        return c | (1<<5);
}

//! Will return an empty character with the same sign/capitalization as `c`.
char GetSign(char c) {
    if (IsUpper(c))
        return UPPERCASE;
    else
        return LOWERCASE;
}

//! Will return `c` with the same capitalization as `sign`.
char CopySign(char sign, char c) {
    if ((!IsLetter(c)) && (!IsGeneric(c)))
        return c;
    if (IsUpper(sign))
        return MakeUpper(c);
    else
        return MakeLower(c);
}

//! Will replace all occurrences of a substring `find` in `str` with `sub`, but it will try to keep the characters signs.
//! Like (pay attention to the capitalization):.
//! ("Hello World", "hello", "hi") -> "Hi World".
//! ("hello World", "hello", "hi") -> "hi World".
//! ("HELLO World", "hello", "hi") -> "HI World".
//! You can also supply a callback to only perform a replacement if certain conditions apply.
std::string ReplaceButKeepSigns(
        const std::string& str,
        std::string find,
        const std::string& sub,
        const std::function<bool(const std::string&, const std::size_t)>& onlyIf = [](const std::string &,
                                                                                      const std::size_t) { return true; } // Default is: replace always
) {

    // Quick accepts-and rejects
    if (str.length() == 0)
        return "";
    else if (find.length() == 0)
        return str;

    std::stringstream ss;

    // Better safe than sorry
    find = StringTools::Lower(find);

    for (std::size_t i = 0; i < str.length(); i++)
    {
        const std::string foundInText = str.substr(i, find.length());
        const std::string foundInText_lower = StringTools::Lower(foundInText);
        if (foundInText_lower == find)
        {
            // Ask the callback if we should replace this one
            if (onlyIf(foundInText, i))
            {
                // Here we've found our occurrence...
                // We have three possible cases:
                // 1: len(find) == len(sub), in this case we want to sync capitalization by index.
                // 2: len(find) < len(sub), in this case we sync by index, BUT...
                // 3: len(find) > len(sub): sync capitalization by index

                // We want to sync capitalization by index
                // This accounts for both cases: 1 and 3
                if (foundInText.length() >= sub.length())
                {
                    for (std::size_t j = 0; j < sub.length(); j++)
                    {
                        const char cf = foundInText[j];
                        const char cs = sub[j];

                        ss << CopySign(cf, cs);
                    }
                }

                // in this case we sync by index, BUT...
                else if (foundInText.length() < sub.length())
                {
                    char followingCharsSign = 0;
                    bool doHaveFollowingChar = false;
                    // Do we even have a following char?
                    if (str.length() >= i + foundInText.length() + 1)
                    {
                        const char followingChar = str[i + foundInText.length()];

                        // Is it a letter?
                        if (IsLetter(followingChar))
                        {
                            // Copy its sign
                            followingCharsSign = GetSign(followingChar);
                            doHaveFollowingChar = true;
                        }
                    }


                    char lastCharCharSign = 0;
                    for (std::size_t j = 0; j < sub.length(); j++)
                    {
                        const char cs = sub[j];

                        // Do we still have chars of 'find' left?
                        if (j < foundInText.length())
                        {
                            // Yes: Just copy the sign as is, and update the last sign seen
                            const char cf = foundInText[j];
                            lastCharCharSign = GetSign(cf);
                            ss << CopySign(cf, cs);
                        }
                        else
                        {
                            // No: Use the last sign seen, or the sign of the following char (the following char within the same word-boundary) (Important for replacing vocals within a word)
                            const char charSignToUse = doHaveFollowingChar ? followingCharsSign : lastCharCharSign;
                            ss << CopySign(charSignToUse, cs);
                        }
                    }
                }
            }
            else
            {
                // We do not have an occurrence... just insert the char as is
                ss << str[i];
            }

            // Advance i accordingly
            i += foundInText.length()-1;
        }
        else
        {
            // We do not have an occurrence... just insert the char as is
            ss << str[i];
        }
    }

    return ss.str();
}

//! Will make a boring string look sooper dooper kawaii and cute :3
std::string MakeUwu(std::string boringString) {
    // Easy ones first
    // none, lol

    // Slightly more complex... Multichar replacements, but we have to keep capitalization...
    boringString = ReplaceButKeepSigns(boringString, "th", "tw");
    boringString = ReplaceButKeepSigns(boringString, "ove", "uv");
    boringString = ReplaceButKeepSigns(boringString, "have", "haf");
    boringString = ReplaceButKeepSigns(boringString, "tr", "tw");
    boringString = ReplaceButKeepSigns(boringString, "up", "uwp");

    // Let's do some language adjustments
    boringString = ReplaceButKeepSigns(boringString, "twank you", "you're twe best <3333 xoxo");
    boringString = ReplaceButKeepSigns(boringString, "good", "sooper dooper");
    boringString = ReplaceButKeepSigns(boringString, "suwper", "sooper dooper");
    boringString = ReplaceButKeepSigns(boringString, "well", "sooper dooper");
    boringString = ReplaceButKeepSigns(boringString, "emacs", "vim");
    boringString = ReplaceButKeepSigns(boringString, "twanks", "you're twe best :DD xoxo");
    boringString = ReplaceButKeepSigns(boringString, "hello", "hiiiiiii");
    boringString = ReplaceButKeepSigns(boringString, "dear", "hiiiiiii");
    boringString = ReplaceButKeepSigns(boringString, "hi", "hiiiiiii");

    // Replace N with Ny, but only if succeeded by a vowel
    boringString = ReplaceButKeepSigns(
            boringString,
            "n",
            "ny",
            [boringString](const std::string &found, int index) {
                // Don't replace, if we are on the last char
                if (index == boringString.length() - 1)
                    return false;

                // Only replace if the next char is a vowel
                const char nextChar = MakeLower(boringString[index + found.length()]);

                // Is this a vowel?
                if (IsVowel(nextChar))
                    return true;

                // Else, don't replace
                return false;
            }
    );

    // Replace R with W, but only if not succeeded by a non-vowel
    boringString = ReplaceButKeepSigns(
            boringString,
            "r",
            "w",
            [boringString](const std::string &found, const std::size_t index) {
                // Don't replace, if we are on the last char
                if (index == boringString.length())
                    return false;

                // Only replace if the next char is a vowel
                const char nextChar = MakeLower(boringString[index + found.length()]);

                // Is this a non-vowel?
                if (!IsVowel(nextChar))
                    return false;

                // Else, replace
                return true;
            }
    );

    // Replace L with W, but only if not followed or preceded by another L
    boringString = ReplaceButKeepSigns(
            boringString,
            "l",
            "w",
            [boringString](const std::string &found, int index) {
                if (boringString.length() < found.length() + 2)
                    return false;

                const char lastChar = MakeLower(boringString[index - 1]);
                const char nextChar = MakeLower(boringString[index + found.length()]);

                return (lastChar != 'l') && (nextChar != 'l');
            }
    );

    // Replace LL with WW, but only if followed by a vowel
    boringString = ReplaceButKeepSigns(
            boringString,
            "ll",
            "ww",
            [boringString](const std::string &found, int index) {
                if (boringString.length() < found.length())
                    return false;

                const char nextChar = MakeLower(boringString[index + found.length()]);

                return IsVowel(nextChar);
            }
    );

    // Replace random punctuation with uwwwwu cute symbols
    // About evewy fifteenth symbol
    std::stringstream ss;
    std::mt19937 rng(69);
    for (const char c : boringString)
    {
        if ((c == '.') && (rng() % 15 == 0))
        {
            ss << " <3333 ^.^ ";
        }
        else if ((c == '!') && (rng() % 15 == 0))
        {
            ss << "!! Thadws impowtant! <3 ";
        }
        else if ((c == ',') && (rng() % 15 == 0))
        {
            ss << " <3 aaaaaand ";
        }
        else if ((c == '?') && (rng() % 15 == 0))
        {
            ss << "?? now tell me! >:( ";
        }
        else
            ss << c;
    }
    boringString = ss.str();

    // Also replace some ascii-"emojis'
    boringString = StringTools::Replace(boringString, ":)", "UwU :D");
    boringString = StringTools::Replace(boringString, ":D", ":3");
    boringString = StringTools::Replace(boringString, ":-)", "UwwwU :3");
    boringString = StringTools::Replace(boringString, "wow", "XDD"); // "lol" -> "wow"
    boringString = StringTools::Replace(boringString, "^^", "^.^ UwU");

    // Some language replacement should happen after these more complex rules
    boringString = ReplaceButKeepSigns(boringString, "c++", "c++ (rust is hella cutewr btw ^^)");


    return boringString;
}

int main(int argc, char** argv) {

    // We have arguments. Uwwuifie these instead
    if (argc > 1)
    {
        // We have to put the args together first, because some replace-rules cross word-borders
        std::stringstream  ss;
        for (std::size_t i = 1; i < argc; i++)
            ss << std::string(argv[i]) + " ";

        std::cout << MakeUwu(ss.str()) << std::endl;
    }

    // Else, be prepared to get __piped__
    else
    {
        // We're better off processing all in one go, as to not having to re-seed the PRNG for every single line...
        // To still support BIG files / endless streams, we'll flush our buffer after every 10th line
        std::string buf;
        std::stringstream ss;
        std::size_t lineCounter = 0;
        while (std::getline(std::cin, buf))
        {
            ss << buf << std::endl;
            lineCounter++;

            if (lineCounter >= 10)
            {
                lineCounter = 0;
                std::cout << MakeUwu(ss.str()) << std::endl;
                ss.str("");
            }
        }

        // Flush one last time
        std::cout << MakeUwu(ss.str()) << std::endl;
    }

    return 0;
}