Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/ylib/core/lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ void println(int val) {
fflush(stdout);
}

void println(long int val) {
fprintf(stdout, "%ld\n", val);
void println(long long val) {
fprintf(stdout, "%lld\n", val);
fflush(stdout);
}

void println(unsigned long int val) {
fprintf(stdout, "%lu\n", val);
void println(unsigned long long val) {
fprintf(stdout, "%llu\n", val);
fflush(stdout);
}

Expand Down
271 changes: 159 additions & 112 deletions include/ylib/core/sutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,145 +3,192 @@
#include <sstream>
#include <algorithm>
#include <optional>
#include <map>
#include <ylib/core/lang.h>

namespace ylib {
namespace core {


string fieldToString(const char* field, const optional<string>& val){
if(val.has_value() == true){
stringstream ss;
ss << field;
ss << ": '";
ss << val.value();
ss << "'";
return ss.str();
}

stringstream ss;
ss << field;
ss << ": ";
ss << "(unassigned)";
return ss.str();
}
namespace ylib::core {

string fieldToString(const char* field, const optional<double>& val){
if(val.has_value() == true){
stringstream ss;
ss << field;
ss << ": ";
ss << val.value();
return ss.str();
}

stringstream ss;
ss << field;
ss << ": ";
ss << "(unassigned)";
return ss.str();
}
using namespace std;


string fieldToString(const char *field, const optional<string> &val) {
if (val.has_value() == true) {
stringstream ss;
ss << field;
ss << ": '";
ss << val.value();
ss << "'";
return ss.str();
}

void findAndReplaceInplace(string& txt, const string& search, const string& replace) {
size_t pos = 0;
while ((pos = txt.find(search, pos)) != std::string::npos) {
txt.replace(pos, search.length(), replace);
pos += replace.length();
stringstream ss;
ss << field;
ss << ": ";
ss << "(unassigned)";
return ss.str();
}
}

string findAndReplace(const string& txt, const string& search, const string& replace){
string copy = txt;
findAndReplaceInplace(copy, search, replace);
return copy;
}

Bool endsWith(string& txt, const char c) {
if (txt.length() == 0) {
return False;
}

size_t idx = txt.length() - 1;
if( txt.at(idx) == c){
return True;
}

return False;
}
string fieldToString(const char *field, const optional<double> &val) {
if (val.has_value() == true) {
stringstream ss;
ss << field;
ss << ": ";
ss << val.value();
return ss.str();
}

stringstream ss;
ss << field;
ss << ": ";
ss << "(unassigned)";
return ss.str();
}

void findAndReplaceInplace(string &txt, const string &search, const string &replace) {
size_t pos = 0;
while ((pos = txt.find(search, pos)) != string::npos) {
txt.replace(pos, search.length(), replace);
pos += replace.length();
}
}

string findAndReplace(const string &txt, const string &search, const string &replace) {
string copy = txt;
findAndReplaceInplace(copy, search, replace);
return copy;
}

Bool endsWith(const std::string& str, const std::string& suffix){
if( str.size() >= suffix.size() &&
0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix)){
return True;
}
Bool endsWith(string &txt, const char c) {
if (txt.length() == 0) {
return False;
}

return False;
}
size_t idx = txt.length() - 1;
if (txt.at(idx) == c) {
return True;
}

Bool endsWith(const std::string& str, const char* suffix){
std::string s = suffix;
return endsWith(str, s);
}
return False;
}


Bool endsWith(const std::string &str, const std::string &suffix) {
if (str.size() >= suffix.size() &&
0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix)) {
return True;
}

return False;
}

Bool endsWith(const std::string &str, const char *suffix) {
std::string s = suffix;
return endsWith(str, s);
}

string repeat(Int16 count, const char c) {
stringstream ss;

for (Int16 i = 0; i < count; i++) {
ss << c;
}
string repeat(Int16 count, const char c) {
stringstream ss;

return ss.str();
}
for (Int16 i = 0; i < count; i++) {
ss << c;
}

string numFill(Int64 num, UInt16 count, const char c) {
checkParamIsPositive("count", count);
return ss.str();
}

string snum = std::to_string(num);
string numFill(Int64 num, UInt16 count, const char c) {
checkParamIsPositive("count", count);

size_t len = snum.length();
string snum = std::to_string(num);

if (len >= (size_t)count) {
//nothing to fill.
return snum;
}
size_t len = snum.length();

size_t diff = count - len;
stringstream ss;
if (len >= (size_t) count) {
//nothing to fill.
return snum;
}


for (size_t i = 0; i < diff; i++) {
ss << c;
}
size_t diff = count - len;
stringstream ss;

ss << snum;
return ss.str();
}

void toLowerInplace(string& txt) {
for (size_t i = 0; i < diff; i++) {
ss << c;
}

std::transform(txt.begin(), txt.end(), txt.begin(), [](int c) {
return std::tolower(c);
});
}
ss << snum;
return ss.str();
}

string toLower(const string& txt) {
string ans = txt;
void toLowerInplace(string &txt) {

std::transform(ans.begin(), ans.end(), ans.begin(), [](int c) {
return std::tolower(c);
});
std::transform(txt.begin(), txt.end(), txt.begin(), [](int c) {
return std::tolower(c);
});
}

return ans;
}
string toLower(const string &txt) {
string ans = txt;

string toLower(const char* txt) {
string stxt{ txt };
return toLower(stxt);
}
std::transform(ans.begin(), ans.end(), ans.begin(), [](int c) {
return std::tolower(c);
});

return ans;
}

}//namespace core
}//namespace ylib
string toLower(const char *txt) {
string stxt{txt};
return toLower(stxt);
}

class ToStringBuilder {
private:
vector<string> keys;
vector<string> values;
void doAppend(string key, string val) {
keys.push_back(key);
values.push_back(val);
}
public:

void append(const char *key, string val) {
string sval = "\"" + val + "\"";
doAppend(string(key), sval);
}

void append(string key, string val){
string sval = "\"" + val + "\"";
doAppend(key, sval);
}

void append(const char *key, UInt64 val) {
doAppend(string(key), std::to_string(val));
}

void append(string key, Int64 val) {
doAppend(string(key), std::to_string(val));
}

string str() {
size_t len = keys.size();
ostringstream oss;
oss << "{";
for (size_t i = 0; i < len; i++) {
string &k = keys[i];
string &v = values[i];

oss << k;
oss << ": ";
oss << v;

if (i + 1 < len) {
oss << ", ";
}
}
oss << "}";
return oss.str();
}
};
}
4 changes: 2 additions & 2 deletions include/ylib/core/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ Represents the current time at GMT-0 up to milliseconds resolution.
validate();
}

Time(tm &t, UInt16 milli) : Time((UInt8) t.tm_hour, (UInt8) t.tm_min, (UInt8) t.tm_sec, (UInt16) milli) {
Time(tm &t, UInt16 milli) : Time((UInt8) t.tm_hour, (UInt8) t.tm_min, (UInt8) t.tm_sec, milli) {
}

Time(UInt8 hour, UInt8 min, UInt8 sec) : Time(hour, min, sec, 0) {
Expand Down Expand Up @@ -522,7 +522,7 @@ Represents the current time, using the current system locale, up to milliseconds
return _time;
}

string toString() {
string toString() const {
return sfput("{} {}", _date.toString(), _time.toString());
}
};
Expand Down
9 changes: 9 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@ docker run -it \
cpplib-core bash -c "
echo \"Docker container created. About to run program main.\" && \
./main
"
echo "====================================================================="
docker run -it \
--rm \
-v ${PWD}/Docker_Debug:${CPD}/Debug \
-w ${CPD}/Debug \
cpplib-core bash -c "
echo \"Docker container created. About to run program test.\" && \
./test
"
40 changes: 40 additions & 0 deletions test-include/ylib/core/StringUtilsTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Created by Yaison Alcantara 3 on 3/2/23.
//

#pragma once

#include <ylib/core/sutils.h>
#include <ylib/test.h>

namespace ylib::core {

using namespace ylib::test;


void testSUtils(){
TEST("test ToStringBuilder", []{
// Given
ToStringBuilder sb1;
string k1 = "pop";
string v1 = "push";

string k2 = "peak";
Int32 v2 = 34;

ToStringBuilder sb2; //empty

// When
sb1.append(k1, v1);
sb1.append(k2, v2);
string ans1 = sb1.str();

string ans2 = sb2.str();

// Then
assertEquals("{pop: \"push\", peak: 34}", ans1);
assertEquals("{}", ans2);
});
}

}
Loading