Implements utils and tests.

This commit is contained in:
2023-04-17 22:58:50 -07:00
parent d7df3a2be4
commit 1f6df1227c
3 changed files with 227 additions and 34 deletions

View File

@@ -21,7 +21,7 @@ namespace SBF {
using std::make_tuple;
using std::vector;
string word_wrap(const string& text, int maxWidth) {
string word_wrap(const string& text, const size_t max_width) {
string output = "";
string thisLine = "";
string nextChunk = "";
@@ -46,13 +46,13 @@ namespace SBF {
thisLine = thisLine + " ";
}
thisLineLength = thisLine.size();
if (nextChunkLength > maxWidth) {
nextChunk = get_substring(text, thisLineCurrentPosition, maxWidth - thisLineLength);
nextSpace = thisLineStartPosition + maxWidth;
if (nextChunkLength > max_width) {
nextChunk = get_substring(text, thisLineCurrentPosition, max_width - thisLineLength);
nextSpace = thisLineStartPosition + max_width;
thisLine = thisLine + nextChunk;
thisLineCurrentPosition = nextSpace;
} else if(thisLineLength + nextChunkLength > maxWidth) {
thisLine = make_fit_l(thisLine, maxWidth, L' ');
} else if(thisLineLength + nextChunkLength > max_width) {
thisLine = make_fit_l(thisLine, max_width, L' ');
} else {
thisLine = thisLine + nextChunk;
thisLineCurrentPosition = nextSpace + 1;
@@ -61,11 +61,11 @@ namespace SBF {
} else {
thisLineCurrentPosition = nextSpace + 1;
}
if (thisLineLength >= maxWidth || thisLineCurrentPosition > textLength) {
if (thisLineLength >= max_width || thisLineCurrentPosition > textLength) {
if (thisLineCurrentPosition > textLength) {
done = true;
}
thisLine = make_fit_l(thisLine, maxWidth, L'_');
thisLine = make_fit_l(thisLine, max_width, L'_');
output += thisLine + (done ? "" : "\n");
thisLine = "";
thisLineLength = thisLine.size();
@@ -76,7 +76,11 @@ namespace SBF {
return output;
}
string string_dollar(size_t length, char ch) {
string string_dollar(const size_t length, const char ch) {
if (ch == '\0') {
return string_dollar(length, ' ');
}
string str = "";
for (size_t i = 0; i<length; i++) {
str += ch;
@@ -84,19 +88,19 @@ namespace SBF {
return str;
}
string left(const string& text, size_t length) {
string left(const string& text, const size_t length) {
return text.substr(0, length);
}
string make_fit_l(const string& text, size_t length, char paddCh) {
return left(text + string_dollar(length, paddCh), length);
string make_fit_l(const string& text, const size_t length, const char pad_character) {
return left(text + string_dollar(length, pad_character != '\0' ? pad_character : ' '), length);
}
string get_substring(const string& text, const int32_t var1, const int32_t var2) {
return text.substr(std::min<int32_t>(var1, text.length()-1), std::max(var2, 0));
string get_substring(const string& text, const size_t start, const size_t length) {
return text.substr(std::min<size_t>(start, text.length()), std::max<size_t>(length, 0));
}
size_t get_index_of(const string& text, const string& search, size_t start) {
size_t get_index_of(const string& text, const string& search, const size_t start) {
return text.find(search, start);
}
} // End namespace SBF