Skip to content

Instantly share code, notes, and snippets.

@dnbaker
Last active September 3, 2018 14:22
Show Gist options
  • Save dnbaker/5872b3be5eddde6c7973388d56f70563 to your computer and use it in GitHub Desktop.
Save dnbaker/5872b3be5eddde6c7973388d56f70563 to your computer and use it in GitHub Desktop.
Shift case
#include <cstdio>
#include <cstring>
#include <random>
#include <cctype>
int main(int argc, char *argv[]) {
std::FILE *ifp = argc > 1 ? std::fopen(argv[1], "rb"): stdin,
*ofp = argc > 2 ? std::fopen(argv[2], "wb"): stdout;
int c;
static constexpr int arr[]{0,32};
std::mt19937_64 mt(argc + (argc == 1 ? 1337: std::strlen(argv[1])));
long long unsigned v = mt();
unsigned char count = 64;
while((c = getc_unlocked(ifp)) >= 0) {
if(std::isalpha(c)) {
c ^= arr[v&1], v>>=1;
if(__builtin_expect(--count == 0, 0))
count = 64, v = mt();
}
std::fputc(c, ofp);
}
if(ifp != stdin) std::fclose(ifp);
if(ofp != stdout) std::fclose(ofp);
}
#if 0
Daniels-MacBook-Pro:caseshift dnb$ cat caseshift.cpp | ./caseshift
#iNCLude <CSTDio>
#InCLudE <cStrING>
#include <RanDoM>
#IncLUDE <CcTyPE>
inT mAin(INT ARGC, ChAr *ARgV[]) {
STD::FIlE *ifp = argc > 1 ? sTd::FoPen(aRgv[1], "Rb"): STdin,
*ofp = Argc > 2 ? STd::FopeN(aRGv[2], "wb"): STdOuT;
INt c;
STATIC cOnStEXPr INt arr[]{0,32};
sTD::MT19937_64 mt(ARGC + (aRgC == 1 ? 1337: STD::StRLen(ARGV[1])));
LONG long unSiGNED V = mt();
uNSIgneD cHAr CoUNT = 64;
wHIlE((c = GETc_unLoCKed(ifP)) >= 0) {
If(std::iSAlpHA(c)) {
c ^= ARr[V&1], v>>=1;
If(__builtin_expECT(--COuNT == 0, 0))
cOunT = 64, v = Mt();
}
StD::fPuTC(c, OFp);
}
iF(IFp != sTDin) STD::FCLoSE(IfP);
If(Ofp != StdouT) sTD::FcloSe(OfP);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment