Skip to content

Commit 862d784

Browse files
committed
Use more utf8::MakeStringViewRange instead of utf8_string_substr.
1 parent e238ae9 commit 862d784

File tree

8 files changed

+29
-25
lines changed

8 files changed

+29
-25
lines changed

src/kana.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ bool KanaConvertor::append(const fcitx::KeyEvent &key, std::string &result,
154154
return append(raw, result, pending);
155155
}
156156

157-
bool KanaConvertor::append(const std::string &raw, std::string &result,
157+
bool KanaConvertor::append(std::string_view raw, std::string &result,
158158
std::string & /*pending*/) {
159159
result = raw;
160160
pending_ = std::string();

src/kana.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "key2kana_base.h"
1212
#include <fcitx/event.h>
1313
#include <string>
14+
#include <string_view>
1415

1516
class AnthyState;
1617

@@ -23,7 +24,7 @@ class KanaConvertor : public Key2KanaConvertorBase {
2324
bool ignore_space = false) override;
2425
bool append(const fcitx::KeyEvent &key, std::string &result,
2526
std::string &pending, std::string &raw) override;
26-
bool append(const std::string &raw, std::string &result,
27+
bool append(std::string_view raw, std::string &result,
2728
std::string &pending) override;
2829
void clear() override;
2930

src/key2kana.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
#include <fcitx-utils/charutils.h>
1717
#include <fcitx-utils/key.h>
1818
#include <fcitx-utils/keysym.h>
19+
#include <fcitx-utils/stringutils.h>
1920
#include <fcitx-utils/utf8.h>
2021
#include <fcitx/event.h>
2122
#include <fcitx/inputmethodmanager.h>
2223
#include <fcitx/instance.h>
2324
#include <string>
25+
#include <string_view>
2426
#include <vector>
2527

2628
Key2KanaConvertor::Key2KanaConvertor(AnthyState &anthy,
@@ -150,9 +152,9 @@ bool CheckLayout(fcitx::Instance *instance) {
150152
return layout == "jp" || layout.starts_with("jp-");
151153
}
152154

153-
bool Key2KanaConvertor::append(const std::string &str, std::string &result,
155+
bool Key2KanaConvertor::append(std::string_view str, std::string &result,
154156
std::string &pending) {
155-
std::string matching_str = pending_ + str;
157+
std::string matching_str = fcitx::stringutils::concat(pending_, str);
156158
Key2KanaRule exact_match;
157159
bool has_partial_match = false;
158160
bool retval = false;
@@ -293,19 +295,18 @@ void Key2KanaConvertor::resetPending(const std::string & /*result*/,
293295
const std::string &raw) {
294296
lastKey_ = fcitx::Key();
295297

296-
for (unsigned int i = 0; i < fcitx::utf8::length(raw); i++) {
298+
for (auto chr : fcitx::utf8::MakeUTF8StringViewRange(raw)) {
297299
std::string res;
298300
std::string pend;
299-
append(util::utf8_string_substr(raw, i, 1), res, pend);
301+
append(chr, res, pend);
300302
}
301303
}
302304

303-
bool Key2KanaConvertor::processPseudoAsciiMode(const std::string &wstr) {
304-
for (unsigned int i = 0; i < wstr.length(); i++) {
305-
if (fcitx::charutils::isupper(wstr[i]) ||
306-
fcitx::charutils::isspace(wstr[i])) {
305+
bool Key2KanaConvertor::processPseudoAsciiMode(std::string_view wstr) {
306+
for (auto chr : wstr) {
307+
if (fcitx::charutils::isupper(chr) || fcitx::charutils::isspace(chr)) {
307308
isInPseudoAsciiMode_ = true;
308-
} else if (wstr[i] & 0x80) {
309+
} else if (chr & 0x80) {
309310
isInPseudoAsciiMode_ = false;
310311
}
311312
}

src/key2kana.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <fcitx-utils/key.h>
1515
#include <fcitx/event.h>
1616
#include <string>
17+
#include <string_view>
1718

1819
class AnthyState;
1920

@@ -26,6 +27,8 @@ class Key2KanaConvertor : public Key2KanaConvertorBase {
2627
bool ignore_space = false) override;
2728
bool append(const fcitx::KeyEvent &key, std::string &result,
2829
std::string &pending, std::string &raw) override;
30+
bool append(std::string_view str, std::string &result,
31+
std::string &pending) override;
2932
void clear() override;
3033

3134
bool isPending() const override;
@@ -35,13 +38,10 @@ class Key2KanaConvertor : public Key2KanaConvertorBase {
3538
const std::string &raw) override;
3639
void setPseudoAsciiMode(int mode) { pseudoAsciiMode_ = mode; }
3740
bool isPseudoAsciiMode() const { return isInPseudoAsciiMode_; }
38-
bool processPseudoAsciiMode(const std::string &wstr) override;
41+
bool processPseudoAsciiMode(std::string_view wstr) override;
3942
void resetPseudoAsciiMode() override;
4043

4144
private:
42-
bool append(const std::string &str, std::string &result,
43-
std::string &pending) override;
44-
4545
Key2KanaTableSet &tables_;
4646

4747
// state

src/key2kana_base.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <fcitx/event.h>
1313
#include <string>
14+
#include <string_view>
1415

1516
class AnthyState;
1617
class AnthyConfig;
@@ -25,7 +26,7 @@ class Key2KanaConvertorBase {
2526
bool ignore_space = false) = 0;
2627
virtual bool append(const fcitx::KeyEvent &key, std::string &result,
2728
std::string &pending, std::string &raw) = 0;
28-
virtual bool append(const std::string &raw, std::string &result,
29+
virtual bool append(std::string_view raw, std::string &result,
2930
std::string &pending) = 0;
3031
virtual void clear() = 0;
3132

@@ -36,7 +37,7 @@ class Key2KanaConvertorBase {
3637
const std::string &raw) = 0;
3738

3839
virtual void resetPseudoAsciiMode() {}
39-
virtual bool processPseudoAsciiMode(const std::string & /*unused*/) {
40+
virtual bool processPseudoAsciiMode(std::string_view /*unused*/) {
4041
return false;
4142
}
4243

src/nicola.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <fcitx-utils/log.h>
2525
#include <fcitx/event.h>
2626
#include <string>
27+
#include <string_view>
2728
#include <vector>
2829

2930
void NicolaTimeoutFunc(void *arg) {
@@ -381,7 +382,7 @@ bool NicolaConvertor::append(const fcitx::KeyEvent &key, std::string &result,
381382
return handleVoicedConsonant(result, pending);
382383
}
383384

384-
bool NicolaConvertor::append(const std::string &raw, std::string &result,
385+
bool NicolaConvertor::append(std::string_view raw, std::string &result,
385386
std::string & /*pending*/) {
386387
result = raw;
387388
pending_ = std::string();

src/nicola.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
#define __FCITX_ANTHY_NICOLA_H__
1111

1212
#include <fcitx-utils/event.h>
13+
#include <fcitx-utils/eventloopinterface.h>
1314
#include <fcitx-utils/key.h>
1415
#include <fcitx/event.h>
16+
#include <memory>
1517
#include <stdint.h>
1618
#include <string>
19+
#include <string_view>
1720
#include <sys/time.h>
1821

1922
#include "key2kana_base.h"
@@ -36,7 +39,7 @@ class NicolaConvertor : public Key2KanaConvertorBase {
3639
bool ignore_space = false) override;
3740
bool append(const fcitx::KeyEvent &key, std::string &result,
3841
std::string &pending, std::string &raw) override;
39-
bool append(const std::string &raw, std::string &result,
42+
bool append(std::string_view raw, std::string &result,
4043
std::string &pending) override;
4144
void clear() override;
4245

@@ -62,7 +65,6 @@ class NicolaConvertor : public Key2KanaConvertorBase {
6265
bool stop();
6366
int thumbKey(const fcitx::KeyEvent &key);
6467

65-
private:
6668
Key2KanaTableSet &tables_;
6769

6870
// state

src/reading.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ static std::string_view find_romaji(std::string_view c) {
3333
return {};
3434
}
3535

36-
static void to_half(std::string &dest, std::string &src) {
36+
static void to_half(std::string &dest, std::string_view src) {
3737
const auto &table = fcitx_anthy_wide_table;
3838

39-
for (unsigned int i = 0; i < fcitx::utf8::length(src); i++) {
39+
for (auto kana : fcitx::utf8::MakeUTF8StringViewRange(src)) {
4040
bool found = false;
41-
std::string kana = util::utf8_string_substr(src, i, 1);
4241
for (const auto &item : table) {
4342
if (kana == item.wide) {
4443
dest += item.code;
@@ -63,8 +62,7 @@ void ReadingSegment::split(ReadingSegments &segments) {
6362
to_half(half, kana);
6463
bool same_with_raw = half == raw;
6564

66-
for (unsigned int i = 0; i < fcitx::utf8::length(kana); i++) {
67-
std::string c = util::utf8_string_substr(kana, i, 1);
65+
for (auto c : fcitx::utf8::MakeUTF8StringViewRange(kana)) {
6866
ReadingSegment seg;
6967
seg.kana = c;
7068
if (same_with_raw) {

0 commit comments

Comments
 (0)