Commit f514d7d71e2aa288eef6952165ea826f95496091
1 parent
0cd24a7e
start converting to String for Unicode
Showing
1 changed file
with
45 additions
and
12 deletions
src/cxxopts.hpp
| ... | ... | @@ -34,6 +34,37 @@ THE SOFTWARE. |
| 34 | 34 | #include <string> |
| 35 | 35 | #include <vector> |
| 36 | 36 | |
| 37 | +#ifdef CXXOPTS_USE_UNICODE | |
| 38 | +#include <unistr.h> | |
| 39 | + | |
| 40 | +namespace cxxopts | |
| 41 | +{ | |
| 42 | + typedef icu::UnicodeString String; | |
| 43 | + | |
| 44 | + inline | |
| 45 | + String | |
| 46 | + toLocalString(std::string s) | |
| 47 | + { | |
| 48 | + return icu::UnicodeString::fromUTF8(s); | |
| 49 | + } | |
| 50 | +} | |
| 51 | + | |
| 52 | +#else | |
| 53 | + | |
| 54 | +namespace cxxopts | |
| 55 | +{ | |
| 56 | + typedef std::string String; | |
| 57 | + | |
| 58 | + inline | |
| 59 | + String | |
| 60 | + toLocalString(std::string s) | |
| 61 | + { | |
| 62 | + return std::move(s); | |
| 63 | + } | |
| 64 | +} | |
| 65 | + | |
| 66 | +#endif | |
| 67 | + | |
| 37 | 68 | namespace cxxopts |
| 38 | 69 | { |
| 39 | 70 | class Value |
| ... | ... | @@ -284,7 +315,7 @@ namespace cxxopts |
| 284 | 315 | public: |
| 285 | 316 | OptionDetails |
| 286 | 317 | ( |
| 287 | - const std::string& description, | |
| 318 | + const String& description, | |
| 288 | 319 | std::shared_ptr<const Value> value |
| 289 | 320 | ) |
| 290 | 321 | : m_desc(description) |
| ... | ... | @@ -293,7 +324,7 @@ namespace cxxopts |
| 293 | 324 | { |
| 294 | 325 | } |
| 295 | 326 | |
| 296 | - const std::string& | |
| 327 | + const String& | |
| 297 | 328 | description() const |
| 298 | 329 | { |
| 299 | 330 | return m_desc; |
| ... | ... | @@ -326,7 +357,7 @@ namespace cxxopts |
| 326 | 357 | } |
| 327 | 358 | |
| 328 | 359 | private: |
| 329 | - std::string m_desc; | |
| 360 | + String m_desc; | |
| 330 | 361 | std::shared_ptr<const Value> m_value; |
| 331 | 362 | int m_count; |
| 332 | 363 | }; |
| ... | ... | @@ -335,7 +366,7 @@ namespace cxxopts |
| 335 | 366 | { |
| 336 | 367 | std::string s; |
| 337 | 368 | std::string l; |
| 338 | - std::string desc; | |
| 369 | + String desc; | |
| 339 | 370 | bool has_arg; |
| 340 | 371 | }; |
| 341 | 372 | |
| ... | ... | @@ -371,7 +402,7 @@ namespace cxxopts |
| 371 | 402 | const std::string& group, |
| 372 | 403 | const std::string& s, |
| 373 | 404 | const std::string& l, |
| 374 | - const std::string& desc, | |
| 405 | + std::string desc, | |
| 375 | 406 | std::shared_ptr<const Value> value |
| 376 | 407 | ); |
| 377 | 408 | |
| ... | ... | @@ -502,7 +533,7 @@ namespace cxxopts |
| 502 | 533 | std::basic_regex<char> option_specifier |
| 503 | 534 | ("(([a-zA-Z]),)?([a-zA-Z0-9][-_a-zA-Z0-9]+)"); |
| 504 | 535 | |
| 505 | - std::string | |
| 536 | + String | |
| 506 | 537 | format_option |
| 507 | 538 | ( |
| 508 | 539 | const std::string& s, |
| ... | ... | @@ -534,15 +565,15 @@ namespace cxxopts |
| 534 | 565 | return result; |
| 535 | 566 | } |
| 536 | 567 | |
| 537 | - std::string | |
| 568 | + String | |
| 538 | 569 | format_description |
| 539 | 570 | ( |
| 540 | - const std::string& text, | |
| 571 | + const String& text, | |
| 541 | 572 | int start, |
| 542 | 573 | int width |
| 543 | 574 | ) |
| 544 | 575 | { |
| 545 | - std::string result; | |
| 576 | + String result; | |
| 546 | 577 | |
| 547 | 578 | auto current = text.begin(); |
| 548 | 579 | auto startLine = current; |
| ... | ... | @@ -808,11 +839,12 @@ Options::add_option |
| 808 | 839 | const std::string& group, |
| 809 | 840 | const std::string& s, |
| 810 | 841 | const std::string& l, |
| 811 | - const std::string& desc, | |
| 842 | + std::string desc, | |
| 812 | 843 | std::shared_ptr<const Value> value |
| 813 | 844 | ) |
| 814 | 845 | { |
| 815 | - auto option = std::make_shared<OptionDetails>(desc, value); | |
| 846 | + auto stringDesc = toLocalString(std::move(desc)); | |
| 847 | + auto option = std::make_shared<OptionDetails>(stringDesc, value); | |
| 816 | 848 | |
| 817 | 849 | if (s.size() > 0) |
| 818 | 850 | { |
| ... | ... | @@ -826,7 +858,8 @@ Options::add_option |
| 826 | 858 | |
| 827 | 859 | //add the help details |
| 828 | 860 | auto& options = m_help[group]; |
| 829 | - options.options.emplace_back(HelpOptionDetails{s, l, desc, value->has_arg()}); | |
| 861 | + options.options. | |
| 862 | + emplace_back(HelpOptionDetails{s, l, stringDesc, value->has_arg()}); | |
| 830 | 863 | } |
| 831 | 864 | |
| 832 | 865 | void | ... | ... |