Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-manias committed Dec 22, 2024
1 parent 816da3e commit d9b923b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 47 deletions.
52 changes: 22 additions & 30 deletions include/parasol/strings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void split(InType Input, OutIt Output, char Sep = ',') noexcept
auto end = Input.end();
auto current = begin;
while (begin != end) {
if (*begin == Sep) {
if (*begin IS Sep) {
*Output++ = std::string(current, begin);
current = ++begin;
}
Expand All @@ -29,45 +29,40 @@ void split(InType Input, OutIt Output, char Sep = ',') noexcept
inline void ltrim(std::string &String, const std::string &Whitespace = " \n\r\t") noexcept
{
const auto start = String.find_first_not_of(Whitespace);
if ((start != std::string::npos) and (start != 0)) String.erase(0, start);
if (start != std::string::npos) String.erase(0, start);
}

inline void rtrim(std::string &String, const std::string &Whitespace = " \n\r\t") noexcept
{
const auto end = String.find_last_not_of(Whitespace);
if ((end != std::string::npos) and (end != String.size()-1)) String.erase(end+1, String.size()-end);
if (end != std::string::npos) String.erase(end + 1);
}

inline void trim(std::string &String, const std::string &Whitespace = " \n\r\t") noexcept
{
const auto start = String.find_first_not_of(Whitespace);
if ((start != std::string::npos) and (start != 0)) String.erase(0, start);

const auto end = String.find_last_not_of(Whitespace);
if ((end != std::string::npos) and (end != String.size()-1)) String.erase(end+1, String.size()-end);
ltrim(String, Whitespace);
rtrim(String, Whitespace);
}

inline void camelcase(std::string &s) noexcept {
bool raise = true;
for (std::size_t i=0; i < s.size(); i++) {
for (auto &ch : s) {
if (raise) {
s[i] = std::toupper(s[i]);
ch = std::toupper(ch);
raise = false;
}
else if (s[i] <= 0x20) raise = true;
else if (unsigned(ch) <= 0x20) raise = true;
}
}

// Case-insensitive string comparison, both of which must be the same length.

[[nodiscard]] inline bool iequals(const std::string_view lhs, const std::string_view rhs) noexcept
{
auto ichar_equals = [](char a, char b) {
return std::tolower((unsigned char)(a)) == std::tolower((unsigned char)(b));
};

if (lhs.size() != rhs.size()) return false;
return std::ranges::equal(lhs, rhs, ichar_equals);
return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](char a, char b) {
return std::tolower(static_cast<unsigned char>(a)) IS std::tolower(static_cast<unsigned char>(b));
});
}

[[nodiscard]] inline bool wildcmp(const std::string_view Wildcard, std::string_view String, bool Case = false) noexcept
Expand All @@ -80,7 +75,7 @@ inline void camelcase(std::string &s) noexcept {
while ((w < Wildcard.size()) and (s < String.size())) {
bool fail = false;
if (Wildcard[w] IS '*') {
while ((Wildcard[w] IS '*') and (w < Wildcard.size())) w++;
while (w < Wildcard.size() and Wildcard[w] IS '*') w++;
if (w IS Wildcard.size()) return true; // Wildcard terminated with a '*'; rest of String will match.

auto i = Wildcard.find_first_of("*|", w); // Count the printable characters after the '*'
Expand All @@ -99,8 +94,8 @@ inline void camelcase(std::string &s) noexcept {
if (Wildcard[w] IS String[s]) break;
}
else {
auto char1 = Wildcard[w]; if ((char1 >= 'A') and (char1 <= 'Z')) char1 = char1 - 'A' + 'a';
auto char2 = String[s]; if ((char2 >= 'A') and (char2 <= 'Z')) char2 = char2 - 'A' + 'a';
auto char1 = std::tolower(static_cast<unsigned char>(Wildcard[w]));
auto char2 = std::tolower(static_cast<unsigned char>(String[s]));
if (char1 IS char2) break;
}
s++;
Expand All @@ -117,8 +112,8 @@ inline void camelcase(std::string &s) noexcept {
if (Wildcard[w++] != String[s++]) fail = true;
}
else {
auto char1 = Wildcard[w++]; if ((char1 >= 'A') and (char1 <= 'Z')) char1 = char1 - 'A' + 'a';
auto char2 = String[s++]; if ((char2 >= 'A') and (char2 <= 'Z')) char2 = char2 - 'A' + 'a';
auto char1 = std::tolower(static_cast<unsigned char>(Wildcard[w++]));
auto char2 = std::tolower(static_cast<unsigned char>(String[s++]));
if (char1 != char2) fail = true;
}
}
Expand All @@ -131,8 +126,8 @@ inline void camelcase(std::string &s) noexcept {
if (Wildcard[w++] != String[s++]) fail = true;
}
else {
auto char1 = Wildcard[w++]; if ((char1 >= 'A') and (char1 <= 'Z')) char1 = char1 - 'A' + 'a';
auto char2 = String[s++]; if ((char2 >= 'A') and (char2 <= 'Z')) char2 = char2 - 'A' + 'a';
auto char1 = std::tolower(static_cast<unsigned char>(Wildcard[w++]));
auto char2 = std::tolower(static_cast<unsigned char>(String[s++]));
if (char1 != char2) fail = true;
}
}
Expand All @@ -149,8 +144,7 @@ inline void camelcase(std::string &s) noexcept {
}

if (String.size() IS s) {
if (Wildcard.size() IS w) return true;
else if (Wildcard[w] IS '|') return true;
if (w IS Wildcard.size() or Wildcard[w] IS '|') return true;
}

if ((w < Wildcard.size()) and (Wildcard[w] IS '*')) return true;
Expand All @@ -163,11 +157,9 @@ inline void camelcase(std::string &s) noexcept {
[[nodiscard]] inline bool startswith(const std::string_view StringA, const std::string_view StringB) noexcept
{
if (StringA.size() > StringB.size()) return false;
std::size_t i;
for (i = 0; i < StringA.size(); i++) {
if (std::tolower(StringA[i]) != std::tolower(StringB[i])) return false;
}
return true;
return std::equal(StringA.begin(), StringA.end(), StringB.begin(), [](char a, char b) {
return std::tolower(static_cast<unsigned char>(a)) IS std::tolower(static_cast<unsigned char>(b));
});
}

[[nodiscard]] inline bool startswith(const std::string_view StringA, CSTRING StringB) noexcept
Expand Down
6 changes: 3 additions & 3 deletions src/core/fs_resolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ ERR ResolvePath(const std::string_view &pPath, RSF Flags, std::string *Result)

bool resolved = false;
#ifdef _WIN32
if ((std::tolower(Path[0]) >= 'a') and (std::tolower(Path[0]) <= 'z') and (Path[1] IS ':')) {
resolved = true; // Windows drive letter reference discovered
if ((Path[2] != '/') and (Path[2] != '\\')) {
if ((Path.size() >= 2) and (std::tolower(Path[0]) >= 'a') and (std::tolower(Path[0]) <= 'z') and (Path[1] IS ':')) {
resolved = true;
if ((Path.size() >= 3) and (Path[2] != '/') and (Path[2] != '\\')) {
// Ensure that the path is correctly formed in order to pass test_path()
src = { Path[0], ':', '\\' };
src.append(Path, 2, std::string::npos);
Expand Down
14 changes: 6 additions & 8 deletions src/core/lib_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2402,20 +2402,18 @@ ERR fs_getinfo(std::string_view Path, FileInfo *Info, LONG InfoSize)
}
}

for (len=0; Path[len]; len++);

if ((Path[len-1] IS '/') or (Path[len-1] IS '\\')) Info->Flags |= RDF::FOLDER|RDF::TIME;
if (Path.ends_with('/') or Path.ends_with('\\')) Info->Flags |= RDF::FOLDER|RDF::TIME;
else if (dir) Info->Flags |= RDF::FOLDER|RDF::TIME;
else Info->Flags |= RDF::FILE|RDF::SIZE|RDF::TIME;

// Extract the file name

i = len;
if ((Path[i-1] IS '/') or (Path[i-1] IS '\\')) i--;

while ((i > 0) and (Path[i-1] != '/') and (Path[i-1] != '\\') and (Path[i-1] != ':')) i--;
size_t fi;
if (Path.ends_with('/') or Path.ends_with('\\')) fi = Path.find_last_of("/\\:", Path.size()-2);
else fi = Path.find_last_of("/\\:");

i = strcopy(Path.data() + i, Info->Name, MAX_FILENAME-2);
if (fi IS std::string::npos) i = strcopy(Path.data(), Info->Name, MAX_FILENAME - 2);
else i = strcopy(Path.data() + fi + 1, Info->Name, MAX_FILENAME - 2);

if ((Info->Flags & RDF::FOLDER) != RDF::NIL) {
if (Info->Name[i-1] IS '\\') Info->Name[i-1] = '/';
Expand Down
2 changes: 1 addition & 1 deletion src/document/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ static ERR insert_text(extDocument *Self, RSTREAM *Stream, stream_char &Index, c
auto ws = Self->NoWhitespace; // Retrieve previous whitespace state
for (unsigned i=0; i < Text.size(); ) {
if (unsigned(Text[i]) <= 0x20) { // Whitespace encountered
for (++i; (unsigned(Text[i]) <= 0x20) and (i < Text.size()); i++);
for (++i; (i < Text.size()) and (unsigned(Text[i]) <= 0x20); i++);
if (!ws) et.text += ' ';
ws = true;
}
Expand Down
10 changes: 5 additions & 5 deletions src/svg/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1631,18 +1631,18 @@ static void process_pattern(extSVG *Self, XMLTag &Tag)
// objectBoundingBox: Coordinate values are relative (0 - 1.0) to the bounding box of the requesting element.
// Implementing this means allocating a 1x1 viewbox for the content, then stretching it to fit the parent element.

if (iequals("userSpaceOnUse", val)) pattern->ContentUnits = VUNIT::USERSPACE;
if (iequals("userSpaceOnUse", val)) pattern->setContentUnits(VUNIT::USERSPACE);
else if (iequals("objectBoundingBox", val)) {
pattern->ContentUnits = VUNIT::BOUNDING_BOX;
pattern->setContentUnits(VUNIT::BOUNDING_BOX);
viewport->setFields(fl::ViewX(0), fl::ViewY(0), fl::ViewWidth(1.0), fl::ViewHeight(1.0));
}
break;

case SVF_PATTERNUNITS:
// 'userSpace' is a deprecated option from SVG 1.0 - perhaps due to the introduction of patternContentUnits.
if (iequals("userSpaceOnUse", val)) { rel_coords = false; pattern->Units = VUNIT::USERSPACE; }
else if (iequals("objectBoundingBox", val)) { rel_coords = true; pattern->Units = VUNIT::BOUNDING_BOX; }
else if (iequals("userSpace", val)) { rel_coords = false; pattern->Units = VUNIT::USERSPACE; }
if (iequals("userSpaceOnUse", val)) { rel_coords = false; pattern->setUnits(VUNIT::USERSPACE); }
else if (iequals("objectBoundingBox", val)) { rel_coords = true; pattern->setUnits(VUNIT::BOUNDING_BOX); }
else if (iequals("userSpace", val)) { rel_coords = false; pattern->setUnits(VUNIT::USERSPACE); }
break;

case SVF_PATTERNTRANSFORM: pattern->setTransform(val); break;
Expand Down

0 comments on commit d9b923b

Please sign in to comment.