Commit 9509ca94f2ccbdb6d78ea5370b8af0b1f21f4dbd

Authored by Jarryd Beck
1 parent 9d1a4dfc

unicode

CMakeLists.txt
... ... @@ -23,5 +23,9 @@ project(cxxopts)
23 23  
24 24 set(VERSION "0.0.1")
25 25  
  26 +find_package(PkgConfig)
  27 +
  28 +pkg_check_modules(ICU REQUIRED icu-uc)
  29 +
26 30 add_subdirectory(src)
27 31  
... ...
src/CMakeLists.txt
... ... @@ -21,5 +21,6 @@
21 21 add_executable(example example.cpp)
22 22  
23 23 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
  24 +target_link_libraries(example ${ICU_LIBRARIES})
24 25  
25 26 install(FILES cxxopts.hpp DESTINATION include)
... ...
src/cxxopts.hpp
... ... @@ -94,6 +94,55 @@ namespace cxxopts
94 94 const icu::UnicodeString* s;
95 95 int32_t i;
96 96 };
  97 +
  98 + inline
  99 + String&
  100 + stringAppend(String&s, String a)
  101 + {
  102 + return s.append(std::move(a));
  103 + }
  104 +
  105 + inline
  106 + String&
  107 + stringAppend(String& s, int n, UChar32 c)
  108 + {
  109 + for (int i = 0; i != n; ++i)
  110 + {
  111 + s.append(c);
  112 + }
  113 +
  114 + return s;
  115 + }
  116 +
  117 + template <typename Iterator>
  118 + String&
  119 + stringAppend(String& s, Iterator begin, Iterator end)
  120 + {
  121 + while (begin != end)
  122 + {
  123 + s.append(*begin);
  124 + ++begin;
  125 + }
  126 +
  127 + return s;
  128 + }
  129 +
  130 + inline
  131 + size_t
  132 + stringLength(const String& s)
  133 + {
  134 + return s.length();
  135 + }
  136 +
  137 + inline
  138 + std::string
  139 + toUTF8String(const String& s)
  140 + {
  141 + std::string result;
  142 + s.toUTF8String(result);
  143 +
  144 + return result;
  145 + }
97 146 }
98 147  
99 148 namespace std
... ... @@ -130,6 +179,35 @@ namespace cxxopts
130 179 {
131 180 return s.length();
132 181 }
  182 +
  183 + inline
  184 + String&
  185 + stringAppend(String&s, String a)
  186 + {
  187 + return s.append(std::move(a));
  188 + }
  189 +
  190 + inline
  191 + String&
  192 + stringAppend(String& s, int n, char c)
  193 + {
  194 + return s.append(n, c);
  195 + }
  196 +
  197 + template <typename Iterator>
  198 + String&
  199 + stringAppend(String& s, Iterator begin, Iterator end)
  200 + {
  201 + return s.append(begin, end);
  202 + }
  203 +
  204 + template <typename T>
  205 + std::string
  206 + toUTF8String(T&& t)
  207 + {
  208 + return std::forward<T>(t);
  209 + }
  210 +
133 211 }
134 212  
135 213 #endif
... ... @@ -452,7 +530,7 @@ namespace cxxopts
452 530  
453 531 Options(std::string program, std::string help_string = "")
454 532 : m_program(std::move(program))
455   - , m_help_string(std::move(help_string))
  533 + , m_help_string(toLocalString(std::move(help_string)))
456 534 {
457 535 }
458 536  
... ... @@ -506,7 +584,7 @@ namespace cxxopts
506 584 parse_positional(std::string option);
507 585  
508 586 inline
509   - String
  587 + std::string
510 588 help(const std::vector<std::string>& groups = {""}) const;
511 589  
512 590 private:
... ... @@ -552,7 +630,7 @@ namespace cxxopts
552 630 help_one_group(const std::string& group) const;
553 631  
554 632 std::string m_program;
555   - std::string m_help_string;
  633 + String m_help_string;
556 634  
557 635 std::map<std::string, std::shared_ptr<OptionDetails>> m_options;
558 636 std::string m_positional;
... ... @@ -661,17 +739,17 @@ namespace cxxopts
661 739 {
662 740 if (lastSpace == startLine)
663 741 {
664   - result.append(startLine, current + 1);
665   - result.append("\n");
666   - result.append(start, ' ');
  742 + stringAppend(result, startLine, current + 1);
  743 + stringAppend(result, "\n");
  744 + stringAppend(result, start, ' ');
667 745 startLine = current + 1;
668 746 lastSpace = startLine;
669 747 }
670 748 else
671 749 {
672   - result.append(startLine, lastSpace);
673   - result.append("\n");
674   - result.append(start, ' ');
  750 + stringAppend(result, startLine, lastSpace);
  751 + stringAppend(result, "\n");
  752 + stringAppend(result, start, ' ');
675 753 startLine = lastSpace + 1;
676 754 }
677 755 size = 0;
... ... @@ -685,7 +763,7 @@ namespace cxxopts
685 763 }
686 764  
687 765 //append whatever is left
688   - result.append(startLine, current);
  766 + stringAppend(result, startLine, current);
689 767  
690 768 return result;
691 769 }
... ... @@ -949,7 +1027,7 @@ Options::add_one_option
949 1027 String
950 1028 Options::help_one_group(const std::string& g) const
951 1029 {
952   - typedef std::vector<std::pair<std::string, std::string>> OptionHelp;
  1030 + typedef std::vector<std::pair<String, String>> OptionHelp;
953 1031  
954 1032 auto group = m_help.find(g);
955 1033 if (group == m_help.end())
... ... @@ -965,14 +1043,14 @@ Options::help_one_group(const std::string&amp; g) const
965 1043  
966 1044 if (!g.empty())
967 1045 {
968   - result += " " + g + " options:\n\n";
  1046 + result += toLocalString(" " + g + " options:\n\n");
969 1047 }
970 1048  
971 1049 for (const auto& o : group->second.options)
972 1050 {
973 1051 auto s = format_option(o.s, o.l, o.has_arg);
974 1052 longest = std::max(longest, stringLength(s));
975   - format.push_back(std::make_pair(s, std::string()));
  1053 + format.push_back(std::make_pair(s, String()));
976 1054 }
977 1055  
978 1056 longest = std::min(longest, static_cast<size_t>(OPTION_LONGEST));
... ... @@ -989,13 +1067,13 @@ Options::help_one_group(const std::string&amp; g) const
989 1067 if (stringLength(fiter->first) > longest)
990 1068 {
991 1069 result += "\n";
992   - result += std::string(longest + OPTION_DESC_GAP, ' ');
  1070 + result += toLocalString(std::string(longest + OPTION_DESC_GAP, ' '));
993 1071 }
994 1072 else
995 1073 {
996   - result += std::string(longest + OPTION_DESC_GAP -
  1074 + result += toLocalString(std::string(longest + OPTION_DESC_GAP -
997 1075 stringLength(fiter->first),
998   - ' ');
  1076 + ' '));
999 1077 }
1000 1078 result += d;
1001 1079 result += "\n";
... ... @@ -1006,10 +1084,10 @@ Options::help_one_group(const std::string&amp; g) const
1006 1084 return result;
1007 1085 }
1008 1086  
1009   -String
  1087 +std::string
1010 1088 Options::help(const std::vector<std::string>& groups) const
1011 1089 {
1012   - std::string result = "Usage:\n " + m_program + " [OPTION...]"
  1090 + String result = "Usage:\n " + toLocalString(m_program) + " [OPTION...]"
1013 1091 + m_help_string + "\n\n";
1014 1092  
1015 1093 for (std::size_t i = 0; i < groups.size(); ++i)
... ... @@ -1021,7 +1099,7 @@ Options::help(const std::vector&lt;std::string&gt;&amp; groups) const
1021 1099 }
1022 1100 }
1023 1101  
1024   - return result;
  1102 + return toUTF8String(result);
1025 1103 }
1026 1104  
1027 1105 }
... ...