Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly nicer string constants in morph::tools #292

Merged
merged 1 commit into from
Nov 27, 2024
Merged
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
49 changes: 29 additions & 20 deletions morph/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <map>
#include <algorithm>
#include <string>
#include <string_view>
#include <cstring>
#include <sstream>
#include <fstream>
Expand Down Expand Up @@ -53,7 +54,9 @@ extern "C" {
/*!
* Character sets useful when calling util::sanitize().
*
* These are ordered so that the most common chars appear earliest.
* These are ordered so that the most common chars appear earliest. Until C++20 (and constexpr
* std::string) I didn't fine a better way to express these (as string_view can't be
* concatenated). They're used to define std::string_views below.
*/
#define CHARS_NUMERIC "0123456789"
#define CHARS_ALPHA "etaoinshrdlcumwfgypbvkjxqzETAOINSHRDLCUMWFGYPBVKJXQZ"
Expand All @@ -63,25 +66,31 @@ extern "C" {
#define CHARS_NUMERIC_ALPHALOWER "etaoinshrdlcumwfgypbvkjxqz0123456789"
#define CHARS_NUMERIC_ALPHAUPPER "0123456789ETAOINSHRDLCUMWFGYPBVKJXQZ"

/*!
* These are the chars which are acceptable for use in both unix, mac AND windows file
* names. This doesn't guarantee a safe Windows filename, as Windows imposes some extra
* conditions (no '.' at end of name, some files such as NUL.txt AUX.txt disallowed).
*/
#define COMMON_FILE_SAFE_CHARS CHARS_NUMERIC_ALPHA"_-.{}^[]`=,;"

/*!
* Chars which are safe for IP domainnames
*/
#define IP_DOMAINNAME_SAFE_CHARS CHARS_NUMERIC_ALPHA"-."

/*!
* Chars which are safe for IP addresses
*/
#define IP_ADDRESS_SAFE_CHARS CHARS_NUMERIC"."

namespace morph
{
/*!
* Chars which are safe for IP domainnames. Allow numeric and alpha chars, the underscore and the
* hyphen. colon is strictly allowed, but best avoided.
*/
static constexpr std::string_view chars_xml_safe {CHARS_NUMERIC_ALPHA"_-"};

/*!
* These are the chars which are acceptable for use in both unix, mac AND windows file
* names. This doesn't guarantee a safe Windows filename, as Windows imposes some extra
* conditions (no '.' at end of name, some files such as NUL.txt AUX.txt disallowed).
*/
static constexpr std::string_view chars_common_file_safe {CHARS_NUMERIC_ALPHA"_-.{}^[]`=,;"};

/*!
* Chars which are safe for IP domainnames
*/
static constexpr std::string_view chars_ip_domainname_safe {CHARS_NUMERIC_ALPHA"-."};

/*!
* Chars which are safe for IP addresses
*/
static constexpr std::string_view chars_ip_address_safe {CHARS_NUMERIC"."};

//! Allows use of transform and tolower() on strings with GNU compiler
struct to_lower { char operator() (const char c) const { return tolower(c); } };

Expand Down Expand Up @@ -607,7 +616,7 @@ namespace morph
void conditionAsFilename (std::string& str)
{
std::string::size_type ptr = std::string::npos;
while ((ptr = str.find_last_not_of (COMMON_FILE_SAFE_CHARS, ptr)) != std::string::npos) {
while ((ptr = str.find_last_not_of (morph::chars_common_file_safe, ptr)) != std::string::npos) {
str[ptr] = '_'; // Replacement character
ptr--;
}
Expand All @@ -625,7 +634,7 @@ namespace morph

// We allow numeric and alpha chars, the underscore and the hyphen. colon
// strictly allowed, but best avoided.
while ((ptr = str.find_last_not_of (CHARS_NUMERIC_ALPHA"_-", ptr)) != std::string::npos) {
while ((ptr = str.find_last_not_of (morph::chars_xml_safe, ptr)) != std::string::npos) {
// Replace the char with an underscore:
str[ptr] = '_';
ptr--;
Expand Down
20 changes: 19 additions & 1 deletion tests/testTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@

int main()
{
int rtn = 0;

std::cout << "pwd: " << morph::tools::getPwd() << std::endl;
return 0;

std::string input_str = "lkajwef7436473723$&\"'.BLAH";
std::string for_filename = input_str;
morph::tools::conditionAsFilename (for_filename);

std::string for_xml = input_str;
morph::tools::conditionAsXmlTag (for_xml);

std::cout << input_str << " conditionAsFilename: " << for_filename << std::endl;
std::string expected_filename = "lkajwef7436473723____.BLAH";
if (expected_filename != for_filename) { --rtn; }

std::cout << input_str << " conditionAsXmlTag: " << for_xml << std::endl;
std::string expected_xml = "lkajwef7436473723_____BLAH";
if (expected_xml != for_xml) { --rtn; }

return rtn;
}
Loading