File tree Expand file tree Collapse file tree 1 file changed +10
-7
lines changed
Expand file tree Collapse file tree 1 file changed +10
-7
lines changed Original file line number Diff line number Diff line change @@ -26,14 +26,15 @@ namespace std {
2626## 例
2727### 基本的な使い方
2828```cpp example
29- #include <iostream>
30- #include <cstdlib>
3129#include <clocale>
30+ #include <cstdlib>
31+ #include <cstring>
32+ #include <iostream>
3233
3334int main() {
3435 std::setlocale(LC_ALL, "ja_JP.UTF-8");
3536 const char *str = "こんにちは";
36- int result = std::mblen(str, MB_CUR_MAX );
37+ int result = std::mblen(str, std::strlen(str) );
3738 std::cout << result << std::endl;
3839 return 0;
3940}
@@ -46,18 +47,20 @@ int main() {
4647
4748### 文字列の文字数を計算する
4849``` cpp example
49- #include < iostream>
50- #include < cstdlib>
5150#include < clocale>
51+ #include < cstdlib>
52+ #include < cstring>
53+ #include < iostream>
5254
5355int count_chars_mblen (const char* s) {
5456 // std::mblen 内部の std::mbstate_t を初期化する必要あり
5557 std::mblen(nullptr, 0);
5658
5759 int count = 0;
5860 std::size_t i = 0;
59- while (s[ i] != '\0') {
60- int len = std::mblen(&s[ i] , MB_CUR_MAX);
61+ std::size_t bytes = std::strlen(s);
62+ while (i < bytes) {
63+ int len = std::mblen(&s[ i] , bytes - i);
6164 if (len < 0) {
6265 len = 1;
6366 }
You can’t perform that action at this time.
0 commit comments