Skip to content

Commit

Permalink
IOS: Adds two helper functions on strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Bénony committed Jan 6, 2016
1 parent 108ce38 commit bbf3785
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions common/str.cpp
Expand Up @@ -665,6 +665,18 @@ String lastPathComponent(const String &path, const char sep) {
return String(first, last);
}

String String::stringByAppendingPathComponent(String component, char sep) const {
if (lastChar() == sep && component.firstChar() == sep) {
return String::format("%s%s", c_str(), component.c_str() + 1);
}

if (lastChar() == sep || component.firstChar() == sep) {
return String::format("%s%s", c_str(), component.c_str());
}

return String::format("%s%c%s", c_str(), sep, component.c_str());
}

String normalizePath(const String &path, const char sep) {
if (path.empty())
return path;
Expand Down
6 changes: 6 additions & 0 deletions common/str.h
Expand Up @@ -180,6 +180,7 @@ class String {
inline uint size() const { return _size; }

inline bool empty() const { return (_size == 0); }
char firstChar() const { return (_size > 0) ? _str[0] : 0; }
char lastChar() const { return (_size > 0) ? _str[_size - 1] : 0; }

char operator[](int idx) const {
Expand Down Expand Up @@ -217,6 +218,11 @@ class String {
*/
void trim();

/**
* Add a path component
*/
String stringByAppendingPathComponent(String component, char sep = '/') const;

uint hash() const;

/**
Expand Down

0 comments on commit bbf3785

Please sign in to comment.