Skip to content

Expand utilities#24

Merged
scheibelp merged 11 commits intospack:mainfrom
johnwparent:util-additions
Mar 31, 2026
Merged

Expand utilities#24
scheibelp merged 11 commits intospack:mainfrom
johnwparent:util-additions

Conversation

@johnwparent
Copy link
Copy Markdown
Collaborator

Utils: Expand api
Adds support for

  • Path canonicalization
  • Making paths absolute
  • Add path padding character to padding signature
  • Stripping quotes
  • Backslash escaping
  • Corrects regex "match" bug
  • Adds error handling to SFN name processing
  • Adds file creation to sfn processing so sfns can be generated for "non existent" files
  • Adds method to validate a path is the proper length
  • Updates path padding to leverage the above api additions
  • Corrects a missing return statement
  • Adds new error types for current and future use

Adds support for

* Path canonicalization
* Making paths absolute
* Add path padding character to padding signature
* Stripping quotes
* Backslash escaping
* Corrects regex "match" bug
* Adds error handling to SFN name processing
* Adds file creation to sfn processing so sfns can be generated for "non existent" files
* Adds method to validate a path is the proper length
* Updates path padding to leverage the above api additions
* Corrects a missing return statement
* Adds new error types for current and future use

Signed-off-by: John Parent <john.parent@kitware.com>
Signed-off-by: John Parent <john.parent@kitware.com>
@johnwparent johnwparent mentioned this pull request Jan 31, 2026
src/utils.cxx Outdated
if (path.length() > MAX_NAME_LEN) {
// Name is too long we need to attempt to shorten
std::string const short_path = short_name(path);
// If new, shortened path is too long, bail
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(question) I'm not clear on how this is bailing (or checking the length of the shortened path).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring should be updated. Originally I was bailing from this method if the SFN couldn't be sufficiently shortened, but now short_name handles that, and this "bails" by letting the exception propagate up.

src/utils.cxx Outdated
result_str = std::string();
} else {
result_str = match.str();
result_str = match.str(1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(question) Is there a reason that e.g. regexSearch isn't also constrained to returning the first group?

Copy link
Copy Markdown
Collaborator Author

@johnwparent johnwparent Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should actually be a bit safer and check whether n > 0 where n is the number of sub matches.

I actually think I should refactor both methods (search and match) to return the match results directly and let callers decide what component to use.

To be clear overall though, regexSearch was not constrained because I was not searching with groups and always wanted the full match as search is more targeted (already matching a substring). It was bad design though.

Signed-off-by: John Parent <john.parent@kitware.com>
Signed-off-by: John Parent <john.parent@kitware.com>
Signed-off-by: John Parent <john.parent@kitware.com>
Signed-off-by: John Parent <john.parent@kitware.com>
Signed-off-by: John Parent <john.parent@kitware.com>
Signed-off-by: John Parent <john.parent@kitware.com>
Signed-off-by: John Parent <john.parent@kitware.com>
Signed-off-by: John Parent <john.parent@kitware.com>
Signed-off-by: John Parent <john.parent@kitware.com>
Copy link
Copy Markdown
Member

@scheibelp scheibelp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving (minor suggestions for later).

// commands to overwrite it
if (!PathFileExistsA(path.c_str())) {
if (!make_file) {
char message[50];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor) IMO it would be good to dynamically allocate based on the length of path (if the user gets a failure here, they wouldn't know which path is the issue or what it belongs to).

* \param bsize the lengh of the padding to add
*/
char* pad_path(const char* pth, DWORD str_size, DWORD bsize) {
char* pad_path(const char* pth, DWORD str_size, char padding_char,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor) many functions return std::string here, which seems like generally the preferred approach (for memory management reasons). Was there a reason for this method specifically to return char*? I'm of the mind that it's worth explaining why whenever we return char* instead of std::string.

// if we have single quotes, strip those
if (startswith(str, "'")) return strip(lstrip(str, "'"), "'");
// if we have double, do that instead.
return strip(lstrip(str, "\""), "\"");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor) Curious what you think should happen for 'hello"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should rename this to "stripquotepairs" as IMO nothing should happen.

Copy link
Copy Markdown
Collaborator Author

@johnwparent johnwparent Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice, it would just strip off only the leading quote, which I don't want.

@scheibelp scheibelp merged commit b6d26aa into spack:main Mar 31, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants