Adds ToLower to utils.
Renames utils functions to match other functions.
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
#define _XOPEN_SOURCE_EXTENDED
|
#define _XOPEN_SOURCE_EXTENDED
|
||||||
#include "Utils.h"
|
#include "Utils.h"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -12,11 +13,10 @@ using namespace SBF;
|
|||||||
namespace SBF {
|
namespace SBF {
|
||||||
namespace {
|
namespace {
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::to_string;
|
|
||||||
using std::vector;
|
using std::vector;
|
||||||
} // End namespace
|
} // 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;
|
vector<string> lines;
|
||||||
string output = "";
|
string output = "";
|
||||||
string thisLine = "";
|
string thisLine = "";
|
||||||
@@ -30,11 +30,11 @@ vector<string> word_wrap(const string& text, const size_t max_width) {
|
|||||||
bool done = false;
|
bool done = false;
|
||||||
|
|
||||||
while (!done) {
|
while (!done) {
|
||||||
nextSpace = get_index_of(text, " ", thisLineCurrentPosition);
|
nextSpace = GetIndexOf(text, " ", thisLineCurrentPosition);
|
||||||
if (nextSpace < 0) {
|
if (nextSpace < 0) {
|
||||||
nextSpace = textLength;
|
nextSpace = textLength;
|
||||||
}
|
}
|
||||||
nextChunk = get_substring(text, thisLineCurrentPosition, nextSpace - thisLineCurrentPosition);
|
nextChunk = GetSubstring(text, thisLineCurrentPosition, nextSpace - thisLineCurrentPosition);
|
||||||
auto nextChunkLength = nextChunk.size();
|
auto nextChunkLength = nextChunk.size();
|
||||||
if (nextChunkLength > 0) {
|
if (nextChunkLength > 0) {
|
||||||
auto needsSpace = thisLine.size() > 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();
|
thisLineLength = thisLine.size();
|
||||||
if (nextChunkLength > max_width) {
|
if (nextChunkLength > max_width) {
|
||||||
nextChunk = get_substring(text, thisLineCurrentPosition, max_width - thisLineLength);
|
nextChunk = GetSubstring(text, thisLineCurrentPosition, max_width - thisLineLength);
|
||||||
nextSpace = thisLineStartPosition + max_width;
|
nextSpace = thisLineStartPosition + max_width;
|
||||||
thisLine = thisLine + nextChunk;
|
thisLine = thisLine + nextChunk;
|
||||||
thisLineCurrentPosition = nextSpace;
|
thisLineCurrentPosition = nextSpace;
|
||||||
} else if (thisLineLength + nextChunkLength > max_width) {
|
} else if (thisLineLength + nextChunkLength > max_width) {
|
||||||
thisLine = make_fit_l(thisLine, max_width, L' ');
|
thisLine = MakeFitL(thisLine, max_width, L' ');
|
||||||
} else {
|
} else {
|
||||||
thisLine = thisLine + nextChunk;
|
thisLine = thisLine + nextChunk;
|
||||||
thisLineCurrentPosition = nextSpace + 1;
|
thisLineCurrentPosition = nextSpace + 1;
|
||||||
@@ -61,7 +61,7 @@ vector<string> word_wrap(const string& text, const size_t max_width) {
|
|||||||
if (thisLineCurrentPosition > textLength) {
|
if (thisLineCurrentPosition > textLength) {
|
||||||
done = true;
|
done = true;
|
||||||
}
|
}
|
||||||
thisLine = make_fit_l(thisLine, max_width, L'_');
|
thisLine = MakeFitL(thisLine, max_width, L'_');
|
||||||
output += thisLine + (done ? "" : "\n");
|
output += thisLine + (done ? "" : "\n");
|
||||||
lines.push_back(thisLine);
|
lines.push_back(thisLine);
|
||||||
thisLine = "";
|
thisLine = "";
|
||||||
@@ -73,9 +73,9 @@ vector<string> word_wrap(const string& text, const size_t max_width) {
|
|||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_dollar(const size_t length, const char ch) {
|
string RepeatChar(const size_t length, const char ch) {
|
||||||
if (ch == '\0') {
|
if (ch == '\0') {
|
||||||
return string_dollar(length, ' ');
|
return RepeatChar(length, ' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
string str = "";
|
string str = "";
|
||||||
@@ -85,11 +85,11 @@ string string_dollar(const size_t length, const char ch) {
|
|||||||
return str;
|
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);
|
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 text_length = text.size();
|
||||||
size_t starting_position = text_length - length;
|
size_t starting_position = text_length - length;
|
||||||
if (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 text_length = text.size();
|
||||||
size_t left_pad_length = length >= text_length ? (length - text_length) / 2 : 0;
|
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;
|
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 left_pad = RepeatChar(left_pad_length, pad_character != '\0' ? pad_character : ' ');
|
||||||
string right_pad = string_dollar(right_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 total_chop = (text_length >= length) ? text_length - length : 0;
|
||||||
size_t left_chop = total_chop / 2; // + 1
|
size_t left_chop = total_chop / 2; // + 1
|
||||||
string ret = left_pad + (text == "" ? "" : text.substr(left_chop, length)) + right_pad;
|
string ret = left_pad + (text == "" ? "" : text.substr(left_chop, length)) + right_pad;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string make_fit_b(const std::string& prefix,
|
std::string MakeFitB(const std::string& prefix,
|
||||||
const std::string& suffix,
|
const std::string& suffix,
|
||||||
const size_t length,
|
const size_t length,
|
||||||
const char pad_character) {
|
const char pad_character) {
|
||||||
return make_fit_l(make_fit_l(prefix, length - suffix.size(), pad_character != '\0' ? pad_character : ' ') + suffix,
|
return MakeFitL(MakeFitL(prefix, length - suffix.size(), pad_character != '\0' ? pad_character : ' ') + suffix,
|
||||||
length,
|
length,
|
||||||
pad_character != '\0' ? pad_character : ' ');
|
pad_character != '\0' ? pad_character : ' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
string make_fit_l(const string& text, const size_t length, const char pad_character) {
|
string MakeFitL(const string& text, const size_t length, const char pad_character) {
|
||||||
return left(text + string_dollar(length, pad_character != '\0' ? pad_character : ' '), length);
|
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) {
|
string MakeFitR(const string& text, const size_t length, const char pad_character) {
|
||||||
return right(string_dollar(length, pad_character != '\0' ? pad_character : ' ') + text, length);
|
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));
|
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);
|
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';
|
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 == "") {
|
if (text == "") {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@@ -156,7 +156,7 @@ string left_trim(const string& text) {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
string right_trim(const string& text) {
|
string RightTrim(const string& text) {
|
||||||
if (text == "") {
|
if (text == "") {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@@ -173,7 +173,9 @@ string right_trim(const string& text) {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string itos(int i) {
|
string ToLower(const string& text) {
|
||||||
return to_string(i);
|
ostringstream os;
|
||||||
|
for_each(text.begin(), text.end(), [&os](unsigned char ch) { os << (char)tolower(ch); });
|
||||||
|
return os.str();
|
||||||
}
|
}
|
||||||
} // End namespace SBF
|
} // End namespace SBF
|
||||||
|
|||||||
@@ -21,41 +21,41 @@ namespace SBF {
|
|||||||
/// @param search The text to search for.
|
/// @param search The text to search for.
|
||||||
/// @param start The position to start searching at.
|
/// @param start The position to start searching at.
|
||||||
/// @return The position of the string if found and std::npos if not found.
|
/// @return The position of the string if found and std::npos if not found.
|
||||||
size_t get_index_of(const std::string& text, const std::string& search, const size_t start);
|
size_t GetIndexOf(const std::string& text, const std::string& search, const size_t start);
|
||||||
|
|
||||||
/// @brief Collapses white space and attempts to word wrap text to a max of max_width columns.
|
/// @brief Collapses white space and attempts to word wrap text to a max of max_width columns.
|
||||||
/// @param text The text to wrap.
|
/// @param text The text to wrap.
|
||||||
/// @param max_width The number of columns to wrap to.
|
/// @param max_width The number of columns to wrap to.
|
||||||
/// @return The wrapped text.
|
/// @return The wrapped text.
|
||||||
std::vector<std::string> word_wrap(const std::string& text, const size_t max_width);
|
std::vector<std::string> WordWrap(const std::string& text, const size_t max_width);
|
||||||
|
|
||||||
/// @brief Gets a substring of another string.
|
/// @brief Gets a substring of another string.
|
||||||
/// @param text The text to split.
|
/// @param text The text to split.
|
||||||
/// @param start The starting position.
|
/// @param start The starting position.
|
||||||
/// @param length The length of the substring.
|
/// @param length The length of the substring.
|
||||||
/// @return The sub string of text.
|
/// @return The sub string of text.
|
||||||
std::string get_substring(const std::string& text, const size_t start, const size_t length);
|
std::string GetSubstring(const std::string& text, const size_t start, const size_t length);
|
||||||
|
|
||||||
/// @brief Pads on the right or truncates text to length using pad_character.
|
/// @brief Pads on the right or truncates text to length using pad_character.
|
||||||
/// @param text The text to operate on.
|
/// @param text The text to operate on.
|
||||||
/// @param length The desired length to make text.
|
/// @param length The desired length to make text.
|
||||||
/// @param pad_character The character to pad with.
|
/// @param pad_character The character to pad with.
|
||||||
/// @return The modified string.
|
/// @return The modified string.
|
||||||
std::string make_fit_l(const std::string& text, const size_t length, const char pad_character = ' ');
|
std::string MakeFitL(const std::string& text, const size_t length, const char pad_character = ' ');
|
||||||
|
|
||||||
/// @brief Pads on both sides or truncates text to length using pad_character.
|
/// @brief Pads on both sides or truncates text to length using pad_character.
|
||||||
/// @param text The text to operate on.
|
/// @param text The text to operate on.
|
||||||
/// @param length The desired length to make text.
|
/// @param length The desired length to make text.
|
||||||
/// @param pad_character The character to pad with.
|
/// @param pad_character The character to pad with.
|
||||||
/// @return The modified string.
|
/// @return The modified string.
|
||||||
std::string make_fit_c(const std::string& text, const size_t length, const char pad_character = ' ');
|
std::string MakeFitC(const std::string& text, const size_t length, const char pad_character = ' ');
|
||||||
|
|
||||||
/// @brief Pads on the left or truncates text to length using pad_character.
|
/// @brief Pads on the left or truncates text to length using pad_character.
|
||||||
/// @param text The text to operate on.
|
/// @param text The text to operate on.
|
||||||
/// @param length The desired length to make text.
|
/// @param length The desired length to make text.
|
||||||
/// @param pad_character The character to pad with.
|
/// @param pad_character The character to pad with.
|
||||||
/// @return The modified string.
|
/// @return The modified string.
|
||||||
std::string make_fit_r(const std::string& text, const size_t length, const char pad_character = ' ');
|
std::string MakeFitR(const std::string& text, const size_t length, const char pad_character = ' ');
|
||||||
|
|
||||||
/// @brief Pads or truncates the space between two strings.
|
/// @brief Pads or truncates the space between two strings.
|
||||||
/// @param prefix The text to put on the left.
|
/// @param prefix The text to put on the left.
|
||||||
@@ -63,7 +63,7 @@ std::string make_fit_r(const std::string& text, const size_t length, const char
|
|||||||
/// @param length The desired length to make the result.
|
/// @param length The desired length to make the result.
|
||||||
/// @param pad_character The character to pad with.
|
/// @param pad_character The character to pad with.
|
||||||
/// @return The modified string.
|
/// @return The modified string.
|
||||||
std::string make_fit_b(const std::string& prefix,
|
std::string MakeFitB(const std::string& prefix,
|
||||||
const std::string& suffix,
|
const std::string& suffix,
|
||||||
const size_t length,
|
const size_t length,
|
||||||
const char pad_character = ' ');
|
const char pad_character = ' ');
|
||||||
@@ -72,32 +72,33 @@ std::string make_fit_b(const std::string& prefix,
|
|||||||
/// @param text The text to operate on.
|
/// @param text The text to operate on.
|
||||||
/// @param length The maximum number of characters to return.
|
/// @param length The maximum number of characters to return.
|
||||||
/// @return The leftmost n characters of text where n is the lesser of text.size and length.
|
/// @return The leftmost n characters of text where n is the lesser of text.size and length.
|
||||||
std::string left(const std::string& text, const size_t length);
|
std::string Left(const std::string& text, const size_t length);
|
||||||
|
|
||||||
/// @brief Gets the rightmost length of characters of text.
|
/// @brief Gets the rightmost length of characters of text.
|
||||||
/// @param text The text to operate on.
|
/// @param text The text to operate on.
|
||||||
/// @param length The maximum number of characters to return.
|
/// @param length The maximum number of characters to return.
|
||||||
/// @return The rightmost n characters of text where n is the lesser of text.size and length.
|
/// @return The rightmost n characters of text where n is the lesser of text.size and length.
|
||||||
std::string right(const std::string& text, const size_t length);
|
std::string Right(const std::string& text, const size_t length);
|
||||||
|
|
||||||
/// @brief Removes whitespace from the left side of text.
|
/// @brief Removes whitespace from the left side of text.
|
||||||
/// @param text The text to operate on.
|
/// @param text The text to operate on.
|
||||||
std::string left_trim(const std::string& text);
|
std::string LeftTrim(const std::string& text);
|
||||||
|
|
||||||
/// @brief Removes whitespace from the right side of text.
|
/// @brief Removes whitespace from the right side of text.
|
||||||
/// @param text The text to operate on.
|
/// @param text The text to operate on.
|
||||||
std::string right_trim(const std::string& text);
|
std::string RightTrim(const std::string& text);
|
||||||
|
|
||||||
/// @brief Gets a string made by repeating a character.
|
/// @brief Gets a string made by repeating a character.
|
||||||
/// @param length The length of the string to return.
|
/// @param length The length of the string to return.
|
||||||
/// @param ch The character to repeat.
|
/// @param ch The character to repeat.
|
||||||
/// @return The repeated string.
|
/// @return The repeated string.
|
||||||
std::string string_dollar(const size_t length, const char ch = ' ');
|
std::string RepeatChar(const size_t length, const char ch = ' ');
|
||||||
|
|
||||||
/// @brief Converts an int to a string.
|
/// @brief Gets the lowercase version of a string. This is a temporary hack until I figure out how to add ICU4C as a
|
||||||
/// @param i The int to convert.
|
/// bazel dependency.
|
||||||
/// @return The string representation of i.
|
/// @param text The text to convert.
|
||||||
std::string itos(int i);
|
/// @return The lowercase version of text. Currently this only supports ASCII characters.
|
||||||
|
std::string ToLower(const std::string& text);
|
||||||
} // End namespace SBF
|
} // End namespace SBF
|
||||||
|
|
||||||
/** @}*/
|
/** @}*/
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ using namespace Test;
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
} // End namespace
|
} // End namespace
|
||||||
|
|
||||||
TestResults test_get_index_of() {
|
TestResults test_GetIndexOf() {
|
||||||
string long_text =
|
string long_text =
|
||||||
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's "
|
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's "
|
||||||
"standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to "
|
"standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to "
|
||||||
@@ -22,8 +22,8 @@ TestResults test_get_index_of() {
|
|||||||
"sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker "
|
"sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker "
|
||||||
"including versions of Lorem Ipsum.";
|
"including versions of Lorem Ipsum.";
|
||||||
return execute_suite<size_t, string, string, size_t>(make_test_suite(
|
return execute_suite<size_t, string, string, size_t>(make_test_suite(
|
||||||
"SBF::Utils::get_index_of",
|
"SBF::Utils::GetIndexOf(const std::string &text, const std::string &search, const size_t start)",
|
||||||
get_index_of,
|
GetIndexOf,
|
||||||
vector<TestTuple<size_t, string, string, size_t>>({
|
vector<TestTuple<size_t, string, string, size_t>>({
|
||||||
make_test<size_t, string, string, size_t>(
|
make_test<size_t, string, string, size_t>(
|
||||||
"should get 0 for the first word in a string", 0, make_tuple(long_text, string("Lorem"), size_t(0))),
|
"should get 0 for the first word in a string", 0, make_tuple(long_text, string("Lorem"), size_t(0))),
|
||||||
@@ -54,10 +54,10 @@ TestResults test_get_index_of() {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
TestResults test_get_substring() {
|
TestResults test_GetSubstring() {
|
||||||
return execute_suite<string, string, size_t, size_t>(make_test_suite(
|
return execute_suite<string, string, size_t, size_t>(make_test_suite(
|
||||||
"SBF::Utils::get_substring",
|
"SBF::Utils::GetSubstring(const std::string &text, const size_t start, const size_t length)",
|
||||||
get_substring,
|
GetSubstring,
|
||||||
vector<TestTuple<string, string, size_t, size_t>>({
|
vector<TestTuple<string, string, size_t, size_t>>({
|
||||||
make_test<string, string, size_t, size_t>(
|
make_test<string, string, size_t, size_t>(
|
||||||
"should get an empty string if start is too big", "", make_tuple(string("asdf"), size_t(6), size_t(2))),
|
"should get an empty string if start is too big", "", make_tuple(string("asdf"), size_t(6), size_t(2))),
|
||||||
@@ -77,10 +77,10 @@ TestResults test_get_substring() {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
TestResults test_left() {
|
TestResults test_Left() {
|
||||||
return execute_suite<string, string, size_t>(make_test_suite(
|
return execute_suite<string, string, size_t>(make_test_suite(
|
||||||
"SBF::Utils::left",
|
"SBF::Utils::Left(const std::string& text, const size_t length)",
|
||||||
SBF::left,
|
Left,
|
||||||
vector<TestTuple<string, string, size_t>>({
|
vector<TestTuple<string, string, size_t>>({
|
||||||
make_test<string, string, size_t>(
|
make_test<string, string, size_t>(
|
||||||
"should get a substring", "Micro", make_tuple(string("Microsoft QBasic"), size_t(5))),
|
"should get a substring", "Micro", make_tuple(string("Microsoft QBasic"), size_t(5))),
|
||||||
@@ -97,10 +97,10 @@ TestResults test_left() {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
TestResults test_right() {
|
TestResults test_Right() {
|
||||||
return execute_suite<string, string, size_t>(make_test_suite(
|
return execute_suite<string, string, size_t>(make_test_suite(
|
||||||
"SBF::Utils::right",
|
"SBF::Utils::Right(const std::string& text, const size_t length)",
|
||||||
SBF::right,
|
Right,
|
||||||
vector<TestTuple<string, string, size_t>>({
|
vector<TestTuple<string, string, size_t>>({
|
||||||
make_test<string, string, size_t>(
|
make_test<string, string, size_t>(
|
||||||
"should get a substring", "Basic", make_tuple(string("Microsoft QBasic"), size_t(5))),
|
"should get a substring", "Basic", make_tuple(string("Microsoft QBasic"), size_t(5))),
|
||||||
@@ -117,10 +117,15 @@ TestResults test_right() {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
TestResults test_make_fit_c() {
|
TestResults test_MakeFitB() {
|
||||||
|
return TestResults().skip(
|
||||||
|
"SBF::Utils::MakeFitB(const std::string &prefix, const std::string &suffix, const size_t length)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TestResults test_MakeFitC() {
|
||||||
return execute_suite<string, string, int32_t, char>(make_test_suite(
|
return execute_suite<string, string, int32_t, char>(make_test_suite(
|
||||||
"SBF::Utils::make_fit_c",
|
"SBF::Utils::MakeFitC(const std::string &text, const size_t length)",
|
||||||
make_fit_c,
|
MakeFitC,
|
||||||
vector<TestTuple<string, string, int32_t, char>>({
|
vector<TestTuple<string, string, int32_t, char>>({
|
||||||
make_test<string, string, int32_t, char>(
|
make_test<string, string, int32_t, char>(
|
||||||
"should truncate a string that is too long", "soft ", make_tuple(string("Microsoft QBasic"), 5, 'A')),
|
"should truncate a string that is too long", "soft ", make_tuple(string("Microsoft QBasic"), 5, 'A')),
|
||||||
@@ -136,10 +141,10 @@ TestResults test_make_fit_c() {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
TestResults test_make_fit_l() {
|
TestResults test_MakeFitL() {
|
||||||
return execute_suite<string, string, int32_t, char>(make_test_suite(
|
return execute_suite<string, string, int32_t, char>(make_test_suite(
|
||||||
"SBF::Utils::make_fit_l",
|
"SBF::Utils::MakeFitL(const std::string &text, const size_t length)",
|
||||||
make_fit_l,
|
MakeFitL,
|
||||||
vector<TestTuple<string, string, int32_t, char>>({
|
vector<TestTuple<string, string, int32_t, char>>({
|
||||||
make_test<string, string, int32_t, char>(
|
make_test<string, string, int32_t, char>(
|
||||||
"should truncate a string that is too long", "Micro", make_tuple(string("Microsoft QBasic"), 5, 'A')),
|
"should truncate a string that is too long", "Micro", make_tuple(string("Microsoft QBasic"), 5, 'A')),
|
||||||
@@ -155,10 +160,10 @@ TestResults test_make_fit_l() {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
TestResults test_make_fit_r() {
|
TestResults test_MakeFitR() {
|
||||||
return execute_suite<string, string, int32_t, char>(make_test_suite(
|
return execute_suite<string, string, int32_t, char>(make_test_suite(
|
||||||
"SBF::Utils::make_fit_r",
|
"SBF::Utils::MakeFitR(const std::string &text, const size_t length)",
|
||||||
make_fit_r,
|
MakeFitR,
|
||||||
vector<TestTuple<string, string, int32_t, char>>({
|
vector<TestTuple<string, string, int32_t, char>>({
|
||||||
make_test<string, string, int32_t, char>(
|
make_test<string, string, int32_t, char>(
|
||||||
"should truncate a string that is too long", "Basic", make_tuple(string("Microsoft QBasic"), 5, 'A')),
|
"should truncate a string that is too long", "Basic", make_tuple(string("Microsoft QBasic"), 5, 'A')),
|
||||||
@@ -174,10 +179,10 @@ TestResults test_make_fit_r() {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
TestResults test_left_trim() {
|
TestResults test_LeftTrim() {
|
||||||
return execute_suite<string, string>(make_test_suite(
|
return execute_suite<string, string>(make_test_suite(
|
||||||
"SBF::Utils::left_trim",
|
"SBF::Utils::LeftTrim(const std::string &text)",
|
||||||
left_trim,
|
LeftTrim,
|
||||||
vector<TestTuple<string, string>>({
|
vector<TestTuple<string, string>>({
|
||||||
make_test<string, string>("should trim a string with spaces",
|
make_test<string, string>("should trim a string with spaces",
|
||||||
"this is a string with spaces on either end ",
|
"this is a string with spaces on either end ",
|
||||||
@@ -205,10 +210,10 @@ TestResults test_left_trim() {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
TestResults test_right_trim() {
|
TestResults test_RightTrim() {
|
||||||
return execute_suite<string, string>(make_test_suite(
|
return execute_suite<string, string>(make_test_suite(
|
||||||
"SBF::Utils::right_trim",
|
"SBF::Utils::RightTrim(const std::string &text)",
|
||||||
right_trim,
|
RightTrim,
|
||||||
vector<TestTuple<string, string>>({
|
vector<TestTuple<string, string>>({
|
||||||
make_test<string, string>("should trim a string with spaces",
|
make_test<string, string>("should trim a string with spaces",
|
||||||
" this is a string with spaces on either end",
|
" this is a string with spaces on either end",
|
||||||
@@ -236,10 +241,10 @@ TestResults test_right_trim() {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
TestResults test_string_dollar() {
|
TestResults test_RepeatChar() {
|
||||||
return execute_suite<string, size_t, char>(make_test_suite(
|
return execute_suite<string, size_t, char>(make_test_suite(
|
||||||
"SBF::Utils::string_dollar",
|
"SBF::Utils::RepeatChar(const size_t length, const char ch)",
|
||||||
string_dollar,
|
RepeatChar,
|
||||||
vector<TestTuple<string, size_t, char>>({
|
vector<TestTuple<string, size_t, char>>({
|
||||||
make_test<string, size_t, char>("should make a string", "YYYYY", make_tuple(size_t(5), 'Y')),
|
make_test<string, size_t, char>("should make a string", "YYYYY", make_tuple(size_t(5), 'Y')),
|
||||||
make_test<string, size_t, char>(
|
make_test<string, size_t, char>(
|
||||||
@@ -248,12 +253,20 @@ TestResults test_string_dollar() {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
TestResults test_word_wrap() {
|
TestResults test_RegexReplace() {
|
||||||
|
return TestResults()
|
||||||
|
.skip(
|
||||||
|
"SBF::Utils::RegexReplace(const string& text, const string& pattern, const string& replace) // TODO: Fill "
|
||||||
|
"this in.")
|
||||||
|
.skip("SBF::Utils::RegexReplace(const string& text, regex regex, const string& replace) // TODO: Fill this in.");
|
||||||
|
}
|
||||||
|
|
||||||
|
TestResults test_WordWrap() {
|
||||||
// TODO: Treat newlines and tabs in text as spaces.
|
// TODO: Treat newlines and tabs in text as spaces.
|
||||||
auto fnToTest = [](string text, int32_t column_width, vector<string> expected) -> string {
|
auto fnToTest = [](string text, int32_t column_width, vector<string> expected) -> string {
|
||||||
ostringstream error_message;
|
ostringstream error_message;
|
||||||
try {
|
try {
|
||||||
vector<string> actual = word_wrap(text, column_width);
|
vector<string> actual = WordWrap(text, column_width);
|
||||||
compare(error_message, expected, actual);
|
compare(error_message, expected, actual);
|
||||||
} catch (const exception& ex) {
|
} catch (const exception& ex) {
|
||||||
error_message << ex.what();
|
error_message << ex.what();
|
||||||
@@ -267,7 +280,7 @@ TestResults test_word_wrap() {
|
|||||||
return "no errors";
|
return "no errors";
|
||||||
};
|
};
|
||||||
return execute_suite<string, string, int32_t, vector<string>>(make_test_suite(
|
return execute_suite<string, string, int32_t, vector<string>>(make_test_suite(
|
||||||
"SBF::Utils::word_wrap",
|
"SBF::Utils::WordWrap(const std::string &text, const size_t max_width)",
|
||||||
fnToTest,
|
fnToTest,
|
||||||
vector<TestTuple<string, string, int32_t, vector<string>>>({
|
vector<TestTuple<string, string, int32_t, vector<string>>>({
|
||||||
make_test<string, string, int32_t, vector<string>>("should return the string if it is shorter than max_width",
|
make_test<string, string, int32_t, vector<string>>("should return the string if it is shorter than max_width",
|
||||||
@@ -301,17 +314,18 @@ TestResults test_word_wrap() {
|
|||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
TestResults results;
|
TestResults results;
|
||||||
|
|
||||||
results += test_get_index_of();
|
results += test_GetIndexOf();
|
||||||
results += test_get_substring();
|
results += test_GetSubstring();
|
||||||
results += test_left();
|
results += test_Left();
|
||||||
results += test_left_trim();
|
results += test_LeftTrim();
|
||||||
results += test_make_fit_c();
|
results += test_MakeFitC();
|
||||||
results += test_make_fit_l();
|
results += test_MakeFitL();
|
||||||
results += test_make_fit_r();
|
results += test_MakeFitR();
|
||||||
results += test_right();
|
results += test_RegexReplace();
|
||||||
results += test_right_trim();
|
results += test_Right();
|
||||||
results += test_string_dollar();
|
results += test_RightTrim();
|
||||||
results += test_word_wrap();
|
results += test_RepeatChar();
|
||||||
|
results += test_WordWrap();
|
||||||
|
|
||||||
PrintResults(cout, results);
|
PrintResults(cout, results);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user