Skip to content

Commit

Permalink
wip - fix string split
Browse files Browse the repository at this point in the history
  • Loading branch information
sbiscigl committed Mar 26, 2024
1 parent 8ff1bec commit efc70bd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/aws-cpp-sdk-core/include/aws/core/http/URI.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace Aws
ss << pathSegments;
Aws::String segments = ss.str();
const auto splitOption = s_preservePathSeparators
? Utils::StringUtils::SplitOptions::PRESERVE_DELIMITERS
? Utils::StringUtils::SplitOptions::INCLUDE_EMPTY_SEGMENTS
: Utils::StringUtils::SplitOptions::NOT_SET;
for (const auto& segment : Aws::Utils::StringUtils::Split(segments, '/', splitOption))
{
Expand Down
12 changes: 5 additions & 7 deletions src/aws-cpp-sdk-core/include/aws/core/utils/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ namespace Aws
*/
NOT_SET,
/**
* Includes empty entries in the vector returned by Split()
* Deprecated use INCLUDE_EMPTY_SEGMENTS instead.
*/
INCLUDE_EMPTY_ENTRIES,
/**
* Include delimeter in vector returned, however removing leading and trailing
* delimiters.
* Include delimeters as empty segments in the split string
*/
PRESERVE_DELIMITERS,
INCLUDE_EMPTY_SEGMENTS,
};

/**
Expand Down Expand Up @@ -122,12 +121,11 @@ namespace Aws
static Aws::Vector<Aws::String> Split(const Aws::String& toSplit, char splitOn, size_t numOfTargetParts, SplitOptions option);

/**
* Splits a string on delimeter, keeping the delimeter in the vector returned.
* Splits a string on delimeter, keeping the delimiter in the string as a empty space.
* @param toSplit, the original string to split
* @param splitOn, the delimiter you want to use.
* @param numOfTargetParts, how many target parts you want to get, if it is 0, as many as possible.
*/
static Aws::Vector<Aws::String> SplitWithDelimiters(const Aws::String& toSplit, char splitOn, size_t numOfTargetParts);
static Aws::Vector<Aws::String> SplitWithSpaces(const Aws::String& toSplit, char splitOn);

/**
* Splits a string on new line characters.
Expand Down
40 changes: 4 additions & 36 deletions src/aws-cpp-sdk-core/source/http/URI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,8 @@ Aws::String URI::GetPath() const

for (auto const& segment : m_pathSegments)
{
if((s_preservePathSeparators && segment == "/"))
{
path.push_back('/');
}
else
{
if (!s_preservePathSeparators)
{
path.push_back('/');
}
path.append(segment);
}
path.push_back('/');
path.append(segment);
}

if (m_pathSegments.empty() || m_pathHasTrailingSlash)
Expand All @@ -230,18 +220,7 @@ Aws::String URI::GetURLEncodedPath() const

for (auto const& segment : m_pathSegments)
{
if((s_preservePathSeparators && segment == "/"))
{
ss << "/";
}
else
{
if (!s_preservePathSeparators)
{
ss << "/";
}
ss << StringUtils::URLEncode(segment.c_str());
}
ss << '/' << StringUtils::URLEncode(segment.c_str());
}

if (m_pathSegments.empty() || m_pathHasTrailingSlash)
Expand All @@ -261,18 +240,7 @@ Aws::String URI::GetURLEncodedPathRFC3986() const
// (mostly; there is some non-standards legacy support that can be disabled)
for (const auto& segment : m_pathSegments)
{
if((s_preservePathSeparators && segment == "/"))
{
ss << "/";
}
else
{
if (!s_preservePathSeparators)
{
ss << "/";
}
ss << urlEncodeSegment(segment, m_useRfcEncoding);
}
ss << '/' << urlEncodeSegment(segment, m_useRfcEncoding);
}

if (m_pathSegments.empty() || m_pathHasTrailingSlash)
Expand Down
28 changes: 9 additions & 19 deletions src/aws-cpp-sdk-core/source/utils/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ Aws::Vector<Aws::String> StringUtils::Split(const Aws::String& toSplit, char spl

Aws::Vector<Aws::String> StringUtils::Split(const Aws::String& toSplit, char splitOn, size_t numOfTargetParts, SplitOptions option)
{
if (option == SplitOptions::PRESERVE_DELIMITERS)
if (option == SplitOptions::INCLUDE_EMPTY_SEGMENTS)
{
return StringUtils::SplitWithDelimiters(toSplit, splitOn, numOfTargetParts);
return StringUtils::SplitWithSpaces(toSplit, splitOn);
}

Aws::Vector<Aws::String> returnValues;
Expand Down Expand Up @@ -133,25 +133,15 @@ Aws::Vector<Aws::String> StringUtils::Split(const Aws::String& toSplit, char spl
return returnValues;
}

Aws::Vector<Aws::String> StringUtils::SplitWithDelimiters(const Aws::String& toSplit, char splitOn, size_t numOfTargetParts)
Aws::Vector<Aws::String> StringUtils::SplitWithSpaces(const Aws::String& toSplit, char splitOn)
{
Aws::Vector<Aws::String> returnValues;
size_t start = 0;
size_t found = toSplit.find_first_of(splitOn, start);

while (returnValues.size() < numOfTargetParts && found != std::string::npos) {
if (found > start) {
returnValues.push_back(toSplit.substr(start, found - start));
}
returnValues.push_back(toSplit.substr(found, 1));
start = found + 1;
found = toSplit.find_first_of(splitOn, start);
size_t pos = 0;
String split{toSplit};
Vector<String> returnValues;
while ((pos = split.find(splitOn)) != std::string::npos) {
returnValues.emplace_back(split.substr(0, pos));
split.erase(0, pos + 1);
}

if (start < toSplit.length()) {
returnValues.push_back(toSplit.substr(start));
}

return returnValues;
}

Expand Down

0 comments on commit efc70bd

Please sign in to comment.