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

COMMON: Add String::forEachLine and convertBiDiStringByLines #2779

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions common/str.cpp
Expand Up @@ -410,6 +410,27 @@ String String::substr(size_t pos, size_t len) const {
return String(_str + pos, MIN((size_t)_size - pos, len));
}

String String::forEachLine(String(*func)(const String, va_list args), ...) const {
String result = "";
size_t index = findFirstOf('\n', 0);
size_t prev_index = 0;
va_list args;
va_start(args, func);
while (index != -1) {
String textLine = substr(prev_index, index - prev_index);
textLine = (*func)(textLine, args);
result = result + textLine + '\n';
prev_index = index + 1;
index = findFirstOf('\n', index + 1);
}

String textLine = substr(prev_index);
textLine = (*func)(textLine, args);
result = result + textLine;
va_end(args);
return result;
}

#pragma mark -

bool operator==(const char* y, const String &x) {
Expand Down
3 changes: 3 additions & 0 deletions common/str.h
Expand Up @@ -242,6 +242,9 @@ class String : public BaseString<char> {
/** Return a substring of this string */
String substr(size_t pos = 0, size_t len = npos) const;

/** Calls func on each line of the string, and returns a joined string */
String forEachLine(String(*func)(const String, va_list args), ...) const;

/** Python-like method **/
U32String decode(CodePage page = kUtf8) const;

Expand Down
9 changes: 9 additions & 0 deletions common/unicode-bidi.cpp
Expand Up @@ -99,6 +99,15 @@ void UnicodeBiDiText::initWithU32String(const U32String &input) {

}

Common::String bidiByLineHelper(Common::String line, va_list args) {
Common::CodePage page = va_arg(args, Common::CodePage);
return Common::convertBiDiString(line, page);
}

String convertBiDiStringByLines(const String &input, const Common::CodePage page) {
return input.forEachLine(bidiByLineHelper, page);
}

String convertBiDiString(const String &input, const Common::Language lang) {
if (lang != Common::HE_ISR) //TODO: modify when we'll support other RTL languages, such as Arabic and Farsi
return input;
Expand Down
4 changes: 4 additions & 0 deletions common/unicode-bidi.h
Expand Up @@ -34,6 +34,7 @@ class UnicodeBiDiText {
uint32 *_log_to_vis_index; // from fribidi conversion
uint32 *_vis_to_log_index; // from fribidi conversion
void initWithU32String(const Common::U32String &str);
Common::String bidiByLine(Common::String line, va_list args);
public:
const Common::U32String logical; // original string, ordered logically
Common::U32String visual; // from fribidi conversion, ordered visually
Expand All @@ -57,6 +58,9 @@ UnicodeBiDiText convertBiDiU32String(const U32String &input);
String convertBiDiString(const String &input, const Common::Language lang);
String convertBiDiString(const String &input, const Common::CodePage page);

// calls convertBiDiString for each line in isolation
String convertBiDiStringByLines(const String &input, const Common::CodePage page);

} // End of namespace Common

#endif