Adds ToLower to utils.

Renames utils functions to match other functions.
This commit is contained in:
2023-04-24 22:09:20 -07:00
parent 5d9a86cb96
commit 482e237fb9
3 changed files with 112 additions and 95 deletions

View File

@@ -1,9 +1,10 @@
#define _XOPEN_SOURCE_EXTENDED
#define _XOPEN_SOURCE_EXTENDED
#include "Utils.h"
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
@@ -12,11 +13,10 @@ using namespace SBF;
namespace SBF {
namespace {
using std::string;
using std::to_string;
using std::vector;
} // End namespace
vector<string> word_wrap(const string& text, const size_t max_width) {
vector<string> WordWrap(const string& text, const size_t max_width) {
vector<string> lines;
string output = "";
string thisLine = "";
@@ -30,11 +30,11 @@ vector<string> word_wrap(const string& text, const size_t max_width) {
bool done = false;
while (!done) {
nextSpace = get_index_of(text, " ", thisLineCurrentPosition);
nextSpace = GetIndexOf(text, " ", thisLineCurrentPosition);
if (nextSpace < 0) {
nextSpace = textLength;
}
nextChunk = get_substring(text, thisLineCurrentPosition, nextSpace - thisLineCurrentPosition);
nextChunk = GetSubstring(text, thisLineCurrentPosition, nextSpace - thisLineCurrentPosition);
auto nextChunkLength = nextChunk.size();
if (nextChunkLength > 0) {
auto needsSpace = thisLine.size() > 0;
@@ -43,12 +43,12 @@ vector<string> word_wrap(const string& text, const size_t max_width) {
}
thisLineLength = thisLine.size();
if (nextChunkLength > max_width) {
nextChunk = get_substring(text, thisLineCurrentPosition, max_width - thisLineLength);
nextChunk = GetSubstring(text, thisLineCurrentPosition, max_width - thisLineLength);
nextSpace = thisLineStartPosition + max_width;
thisLine = thisLine + nextChunk;
thisLineCurrentPosition = nextSpace;
} else if (thisLineLength + nextChunkLength > max_width) {
thisLine = make_fit_l(thisLine, max_width, L' ');
thisLine = MakeFitL(thisLine, max_width, L' ');
} else {
thisLine = thisLine + nextChunk;
thisLineCurrentPosition = nextSpace + 1;
@@ -61,7 +61,7 @@ vector<string> word_wrap(const string& text, const size_t max_width) {
if (thisLineCurrentPosition > textLength) {
done = true;
}
thisLine = make_fit_l(thisLine, max_width, L'_');
thisLine = MakeFitL(thisLine, max_width, L'_');
output += thisLine + (done ? "" : "\n");
lines.push_back(thisLine);
thisLine = "";
@@ -73,9 +73,9 @@ vector<string> word_wrap(const string& text, const size_t max_width) {
return lines;
}
string string_dollar(const size_t length, const char ch) {
string RepeatChar(const size_t length, const char ch) {
if (ch == '\0') {
return string_dollar(length, ' ');
return RepeatChar(length, ' ');
}
string str = "";
@@ -85,11 +85,11 @@ string string_dollar(const size_t length, const char ch) {
return str;
}
string left(const string& text, const size_t length) {
string Left(const string& text, const size_t length) {
return text.substr(0, length);
}
string right(const string& text, const size_t length) {
string Right(const string& text, const size_t length) {
size_t text_length = text.size();
size_t starting_position = text_length - length;
if (text_length >= length) {
@@ -99,40 +99,40 @@ string right(const string& text, const size_t length) {
}
}
string make_fit_c(const string& text, const size_t length, const char pad_character) {
string MakeFitC(const string& text, const size_t length, const char pad_character) {
size_t text_length = text.size();
size_t left_pad_length = length >= text_length ? (length - text_length) / 2 : 0;
size_t right_pad_length = (length >= text_length + left_pad_length) ? (length - text_length - left_pad_length) : 0;
string left_pad = string_dollar(left_pad_length, pad_character != '\0' ? pad_character : ' ');
string right_pad = string_dollar(right_pad_length, pad_character != '\0' ? pad_character : ' ');
string left_pad = RepeatChar(left_pad_length, pad_character != '\0' ? pad_character : ' ');
string right_pad = RepeatChar(right_pad_length, pad_character != '\0' ? pad_character : ' ');
size_t total_chop = (text_length >= length) ? text_length - length : 0;
size_t left_chop = total_chop / 2; // + 1
string ret = left_pad + (text == "" ? "" : text.substr(left_chop, length)) + right_pad;
return ret;
}
std::string make_fit_b(const std::string& prefix,
const std::string& suffix,
const size_t length,
const char pad_character) {
return make_fit_l(make_fit_l(prefix, length - suffix.size(), pad_character != '\0' ? pad_character : ' ') + suffix,
length,
pad_character != '\0' ? pad_character : ' ');
std::string MakeFitB(const std::string& prefix,
const std::string& suffix,
const size_t length,
const char pad_character) {
return MakeFitL(MakeFitL(prefix, length - suffix.size(), pad_character != '\0' ? pad_character : ' ') + suffix,
length,
pad_character != '\0' ? pad_character : ' ');
}
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 MakeFitL(const string& text, const size_t length, const char pad_character) {
return Left(text + RepeatChar(length, pad_character != '\0' ? pad_character : ' '), length);
}
string make_fit_r(const string& text, const size_t length, const char pad_character) {
return right(string_dollar(length, pad_character != '\0' ? pad_character : ' ') + text, length);
string MakeFitR(const string& text, const size_t length, const char pad_character) {
return Right(RepeatChar(length, pad_character != '\0' ? pad_character : ' ') + text, length);
}
string get_substring(const string& text, const size_t start, const size_t length) {
string GetSubstring(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, const size_t start) {
size_t GetIndexOf(const string& text, const string& search, const size_t start) {
return text.find(search, start);
}
@@ -140,7 +140,7 @@ bool is_whitespace(char ch) {
return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\f' || ch == '\v';
}
string left_trim(const string& text) {
string LeftTrim(const string& text) {
if (text == "") {
return "";
}
@@ -156,7 +156,7 @@ string left_trim(const string& text) {
return "";
}
string right_trim(const string& text) {
string RightTrim(const string& text) {
if (text == "") {
return "";
}
@@ -173,7 +173,9 @@ string right_trim(const string& text) {
return "";
}
std::string itos(int i) {
return to_string(i);
string ToLower(const string& text) {
ostringstream os;
for_each(text.begin(), text.end(), [&os](unsigned char ch) { os << (char)tolower(ch); });
return os.str();
}
} // End namespace SBF