Skip to content

Commit

Permalink
Merge pull request #2751 from nvmkuruc/casefoldasciiimpl
Browse files Browse the repository at this point in the history
Add locale independent `TfStringToLowerAscii`

(Internal change: 2301347)
  • Loading branch information
pixar-oss committed Nov 1, 2023
2 parents c749296 + ac0cbb7 commit 5f42eb5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pxr/base/tf/stringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,4 +1201,16 @@ TfGetXmlEscapedString(const std::string &in)
return result;
}

std::string
TfStringToLowerAscii(const std::string& source)
{
std::string folded;
folded.resize(source.size());
std::transform(source.begin(), source.end(), folded.begin(),
[](char ch) {
return ('A' <= ch && ch <= 'Z') ? ch - 'A' + 'a' : ch;
});
return folded;
}

PXR_NAMESPACE_CLOSE_SCOPE
14 changes: 14 additions & 0 deletions pxr/base/tf/stringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ std::string TfStringToUpper(const std::string& source);
TF_API
std::string TfStringCapitalize(const std::string& source);

/// Locale-independent case folding of [A-Z] for ASCII or UTF-8 encoded
/// \p source strings
///
/// This can be used for case insensitive matching where one of the strings
/// being compared either known to be ASCII only by specification (like a URI
/// scheme or an explicit token) or where the specification explicitly notes
/// that only [A-Z] will be matched case insensitively.
///
/// \code
/// TfStringEndsWith(TfStringToLowerAscii("ü.JPG"), ".jpg")
/// \endcode
TF_API
std::string TfStringToLowerAscii(const std::string& source);

/// Trims characters (by default, whitespace) from the left.
///
/// Characters from the beginning of \p s are removed until a character not in
Expand Down
7 changes: 7 additions & 0 deletions pxr/base/tf/testenv/stringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ TestStrings()
TF_AXIOM(TfStringCapitalize("@@@@") == "@@@@");
TF_AXIOM(TfStringCapitalize("") == "");

TF_AXIOM(TfStringToLowerAscii("PIXAR") == TfStringToLowerAscii("pixar"));
TF_AXIOM(TfStringToLowerAscii("PiXaR") == TfStringToLowerAscii("pixar"));
// 'Pixar' in capital Greek letters is not case folded
TF_AXIOM(TfStringToLowerAscii("ΠΙΞΑΡ") == "ΠΙΞΑΡ");
// Mixture of symbols, capital non-ASCII letters, and ASCII letters
TF_AXIOM(TfStringToLowerAscii("ΠΙΞΑΡ ≈ PIXAR") == "ΠΙΞΑΡ ≈ pixar");

TF_AXIOM(TfStringGetSuffix("file.ext") == "ext");
TF_AXIOM(TfStringGetSuffix("here are some words", ' ') == "words");
TF_AXIOM(TfStringGetSuffix("0words", '0') == "words");
Expand Down

0 comments on commit 5f42eb5

Please sign in to comment.