more icu::UnicodeString
This commit is contained in:
parent
f514d7d71e
commit
9d1a4dfcdc
106
src/cxxopts.hpp
106
src/cxxopts.hpp
@ -35,7 +35,7 @@ THE SOFTWARE.
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#ifdef CXXOPTS_USE_UNICODE
|
#ifdef CXXOPTS_USE_UNICODE
|
||||||
#include <unistr.h>
|
#include <unicode/unistr.h>
|
||||||
|
|
||||||
namespace cxxopts
|
namespace cxxopts
|
||||||
{
|
{
|
||||||
@ -47,6 +47,68 @@ namespace cxxopts
|
|||||||
{
|
{
|
||||||
return icu::UnicodeString::fromUTF8(s);
|
return icu::UnicodeString::fromUTF8(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class UnicodeStringIterator : public
|
||||||
|
std::iterator<std::forward_iterator_tag, int32_t>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
UnicodeStringIterator(const icu::UnicodeString* s, int32_t pos)
|
||||||
|
: s(s)
|
||||||
|
, i(pos)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
value_type
|
||||||
|
operator*() const
|
||||||
|
{
|
||||||
|
return s->char32At(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
operator==(const UnicodeStringIterator& rhs) const
|
||||||
|
{
|
||||||
|
return s == rhs.s && i == rhs.i;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
operator!=(const UnicodeStringIterator& rhs) const
|
||||||
|
{
|
||||||
|
return !(*this == rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
UnicodeStringIterator&
|
||||||
|
operator++()
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
UnicodeStringIterator
|
||||||
|
operator+(int32_t v)
|
||||||
|
{
|
||||||
|
return UnicodeStringIterator(s, i + v);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const icu::UnicodeString* s;
|
||||||
|
int32_t i;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
cxxopts::UnicodeStringIterator
|
||||||
|
begin(const icu::UnicodeString& s)
|
||||||
|
{
|
||||||
|
return cxxopts::UnicodeStringIterator(&s, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
cxxopts::UnicodeStringIterator
|
||||||
|
end(const icu::UnicodeString& s)
|
||||||
|
{
|
||||||
|
return cxxopts::UnicodeStringIterator(&s, s.length());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
@ -55,11 +117,18 @@ namespace cxxopts
|
|||||||
{
|
{
|
||||||
typedef std::string String;
|
typedef std::string String;
|
||||||
|
|
||||||
inline
|
template <typename T>
|
||||||
String
|
T
|
||||||
toLocalString(std::string s)
|
toLocalString(T&& t)
|
||||||
{
|
{
|
||||||
return std::move(s);
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline
|
||||||
|
size_t
|
||||||
|
stringLength(const String& s)
|
||||||
|
{
|
||||||
|
return s.length();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -437,7 +506,7 @@ namespace cxxopts
|
|||||||
parse_positional(std::string option);
|
parse_positional(std::string option);
|
||||||
|
|
||||||
inline
|
inline
|
||||||
std::string
|
String
|
||||||
help(const std::vector<std::string>& groups = {""}) const;
|
help(const std::vector<std::string>& groups = {""}) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -479,7 +548,7 @@ namespace cxxopts
|
|||||||
);
|
);
|
||||||
|
|
||||||
inline
|
inline
|
||||||
std::string
|
String
|
||||||
help_one_group(const std::string& group) const;
|
help_one_group(const std::string& group) const;
|
||||||
|
|
||||||
std::string m_program;
|
std::string m_program;
|
||||||
@ -541,11 +610,11 @@ namespace cxxopts
|
|||||||
bool has_arg
|
bool has_arg
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
std::string result = " ";
|
String result = " ";
|
||||||
|
|
||||||
if (s.size() > 0)
|
if (s.size() > 0)
|
||||||
{
|
{
|
||||||
result += "-" + s + ",";
|
result += "-" + toLocalString(s) + ",";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -554,7 +623,7 @@ namespace cxxopts
|
|||||||
|
|
||||||
if (l.size() > 0)
|
if (l.size() > 0)
|
||||||
{
|
{
|
||||||
result += " --" + l;
|
result += " --" + toLocalString(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (has_arg)
|
if (has_arg)
|
||||||
@ -575,13 +644,13 @@ namespace cxxopts
|
|||||||
{
|
{
|
||||||
String result;
|
String result;
|
||||||
|
|
||||||
auto current = text.begin();
|
auto current = std::begin(text);
|
||||||
auto startLine = current;
|
auto startLine = current;
|
||||||
auto lastSpace = current;
|
auto lastSpace = current;
|
||||||
|
|
||||||
int size = 0;
|
int size = 0;
|
||||||
|
|
||||||
while (current != text.end())
|
while (current != std::end(text))
|
||||||
{
|
{
|
||||||
if (*current == ' ')
|
if (*current == ' ')
|
||||||
{
|
{
|
||||||
@ -877,7 +946,7 @@ Options::add_one_option
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string
|
String
|
||||||
Options::help_one_group(const std::string& g) const
|
Options::help_one_group(const std::string& g) const
|
||||||
{
|
{
|
||||||
typedef std::vector<std::pair<std::string, std::string>> OptionHelp;
|
typedef std::vector<std::pair<std::string, std::string>> OptionHelp;
|
||||||
@ -892,7 +961,7 @@ Options::help_one_group(const std::string& g) const
|
|||||||
|
|
||||||
size_t longest = 0;
|
size_t longest = 0;
|
||||||
|
|
||||||
std::string result;
|
String result;
|
||||||
|
|
||||||
if (!g.empty())
|
if (!g.empty())
|
||||||
{
|
{
|
||||||
@ -902,7 +971,7 @@ Options::help_one_group(const std::string& g) const
|
|||||||
for (const auto& o : group->second.options)
|
for (const auto& o : group->second.options)
|
||||||
{
|
{
|
||||||
auto s = format_option(o.s, o.l, o.has_arg);
|
auto s = format_option(o.s, o.l, o.has_arg);
|
||||||
longest = std::max(longest, s.size());
|
longest = std::max(longest, stringLength(s));
|
||||||
format.push_back(std::make_pair(s, std::string()));
|
format.push_back(std::make_pair(s, std::string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -917,14 +986,15 @@ Options::help_one_group(const std::string& g) const
|
|||||||
auto d = format_description(o.desc, longest + OPTION_DESC_GAP, allowed);
|
auto d = format_description(o.desc, longest + OPTION_DESC_GAP, allowed);
|
||||||
|
|
||||||
result += fiter->first;
|
result += fiter->first;
|
||||||
if (fiter->first.size() > longest)
|
if (stringLength(fiter->first) > longest)
|
||||||
{
|
{
|
||||||
result += "\n";
|
result += "\n";
|
||||||
result += std::string(longest + OPTION_DESC_GAP, ' ');
|
result += std::string(longest + OPTION_DESC_GAP, ' ');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result += std::string(longest + OPTION_DESC_GAP - fiter->first.size(),
|
result += std::string(longest + OPTION_DESC_GAP -
|
||||||
|
stringLength(fiter->first),
|
||||||
' ');
|
' ');
|
||||||
}
|
}
|
||||||
result += d;
|
result += d;
|
||||||
@ -936,7 +1006,7 @@ Options::help_one_group(const std::string& g) const
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string
|
String
|
||||||
Options::help(const std::vector<std::string>& groups) const
|
Options::help(const std::vector<std::string>& groups) const
|
||||||
{
|
{
|
||||||
std::string result = "Usage:\n " + m_program + " [OPTION...]"
|
std::string result = "Usage:\n " + m_program + " [OPTION...]"
|
||||||
|
|||||||
@ -24,6 +24,8 @@ THE SOFTWARE.
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#define CXXOPTS_USE_UNICODE
|
||||||
|
|
||||||
#include "cxxopts.hpp"
|
#include "cxxopts.hpp"
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user