migrating conversion modifiers from JetCore::Global to MString.

This commit is contained in:
barant 2024-12-19 10:32:23 -08:00
parent 10df4a6a99
commit f8557aa5f5
4 changed files with 196 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include "MString.h"
#include "Log.h"
#include "uuid/uuid.h"
#include "Exception.h"
#include <cstring>
#include <string>
#include <format>
@ -279,5 +280,130 @@ namespace coreutils {
int MString::offset() {
return cursor - data;
}
MString MString::toBinary() {
push();
reset();
MString target;
char temp;
while(!eod()) {
temp = nextChar();
if(strchr("\\'\".\0\r\n", temp))
target.write('\\');
target.write(temp);
}
pop();
return target;
}
MString MString::fromBinary() {
push();
reset();
MString target;
while(!eod()) {
if(ifNext("\\r"))
target.write(13);
else if(ifNext("\\n"))
target.write(10);
else if(ifNext("\\0"))
target.write(0);
else if(ifNext("\\\\"))
target.write("\\");
else if(ifNext("\\."))
target.write(".");
else if(ifNext("\\\""))
target.write("\"");
else if(ifNext("\\\'"))
target.write("'");
else
target.write(nextChar());
}
pop();
return target;
}
MString MString::toHex() {
push();
reset();
MString target;
char temp;
while(!eod()) {
temp = nextChar();
char temp2 = temp;
temp >>= 4;
target.write(hexChar(temp));
target.write(hexChar(temp2));
}
pop();
return target;
}
MString MString::fromHex() {
push();
reset();
MString target;
while(!eod()) {
char ch1 = nextChar();
ch1 -= 48;
if(ch1 > 9)
ch1 -= 7;
ch1 <<= 4;
ch1 &= 240;
if(eod())
coreutils::Exception("conversion from hex requires even number of characters.");
char ch2 = nextChar();
ch2 -= 48;
if(ch2 > 9)
ch2 -= 7;
ch2 &= 15;
ch1 |= ch2;
target.write(ch1);
}
pop();
return target;
}
MString MString::toBase64() {
MString target;
return target;
}
MString MString::fromBase64() {
MString target;
return target;
}
MString MString::toUpper() {
MString target;
return target;
}
MString MString::toLower() {
MString target;
return target;
}
MString MString::toCGI() {
MString target;
return target;
}
MString MString::fromCGI() {
MString target;
return target;
}
char MString::hexChar(char c) {
c &= 15;
c += 48;
if(c > 57)
c += 7;
return c;
}
}

View File

@ -231,14 +231,81 @@ namespace coreutils {
///
MString &read(int fd);
///
///
///
int offset();
///
///
///
MString toBinary();
///
///
///
MString fromBinary();
///
///
///
MString toHex();
///
///
///
MString fromHex();
///
///
///
MString toBase64();
///
///
///
MString fromBase64();
///
///
///
MString toUpper();
///
///
///
MString toLower();
///
///
///
MString toCGI();
///
///
///
MString fromCGI();
protected:
void setSize(int size);
private:
int bufferSize = 0;
char hexChar(char c);
};
}

Binary file not shown.

View File

@ -94,6 +94,9 @@ int main(int argc, char **argv) {
int test29 = 402;
test28 = test29;
std::cout << "int: " << test28 << std::endl;
coreutils::MString test30 = "ABCDEF";
std::cout << test30 << " to hex " << test30.toHex() << " from hex " << test30.toHex().fromHex() << std::endl;
return 0;
}