00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef XAPIAN_INCLUDED_UNICODE_H
00022 #define XAPIAN_INCLUDED_UNICODE_H
00023
00024 #include <xapian/visibility.h>
00025
00026 #include <string>
00027
00028 namespace Xapian {
00029
00033 class XAPIAN_VISIBILITY_DEFAULT Utf8Iterator {
00034 const unsigned char *p;
00035 const unsigned char *end;
00036 mutable unsigned seqlen;
00037
00038 void calculate_sequence_length() const;
00039
00040 unsigned get_char() const;
00041
00042 Utf8Iterator(const unsigned char *p_, const unsigned char *end_, unsigned seqlen_)
00043 : p(p_), end(end_), seqlen(seqlen_) { }
00044
00045 public:
00047 const char * raw() const {
00048 return reinterpret_cast<const char *>(p ? p : end);
00049 }
00050
00052 size_t left() const { return p ? end - p : 0; }
00053
00065 void assign(const char *p_, size_t len) {
00066 if (len) {
00067 p = reinterpret_cast<const unsigned char*>(p_);
00068 end = p + len;
00069 seqlen = 0;
00070 } else {
00071 p = NULL;
00072 }
00073 }
00074
00085 void assign(const std::string &s) { assign(s.data(), s.size()); }
00086
00095 explicit Utf8Iterator(const char *p_);
00096
00107 Utf8Iterator(const char *p_, size_t len) { assign(p_, len); }
00108
00118 Utf8Iterator(const std::string &s) { assign(s.data(), s.size()); }
00119
00125 Utf8Iterator() : p(NULL), end(0), seqlen(0) { }
00126
00131 unsigned operator*() const;
00132
00137 Utf8Iterator operator++(int) {
00138
00139 if (seqlen == 0) calculate_sequence_length();
00140 const unsigned char *old_p = p;
00141 unsigned old_seqlen = seqlen;
00142 p += seqlen;
00143 if (p == end) p = NULL;
00144 seqlen = 0;
00145 return Utf8Iterator(old_p, end, old_seqlen);
00146 }
00147
00152 Utf8Iterator & operator++() {
00153 if (seqlen == 0) calculate_sequence_length();
00154 p += seqlen;
00155 if (p == end) p = NULL;
00156 seqlen = 0;
00157 return *this;
00158 }
00159
00164 bool operator==(const Utf8Iterator &other) const { return p == other.p; }
00165
00170 bool operator!=(const Utf8Iterator &other) const { return p != other.p; }
00171
00173
00174 typedef std::input_iterator_tag iterator_category;
00175 typedef unsigned value_type;
00176 typedef size_t difference_type;
00177 typedef const unsigned * pointer;
00178 typedef const unsigned & reference;
00180 };
00181
00183 namespace Unicode {
00184
00186 typedef enum {
00187 UNASSIGNED,
00188 UPPERCASE_LETTER,
00189 LOWERCASE_LETTER,
00190 TITLECASE_LETTER,
00191 MODIFIER_LETTER,
00192 OTHER_LETTER,
00193 NON_SPACING_MARK,
00194 ENCLOSING_MARK,
00195 COMBINING_SPACING_MARK,
00196 DECIMAL_DIGIT_NUMBER,
00197 LETTER_NUMBER,
00198 OTHER_NUMBER,
00199 SPACE_SEPARATOR,
00200 LINE_SEPARATOR,
00201 PARAGRAPH_SEPARATOR,
00202 CONTROL,
00203 FORMAT,
00204 PRIVATE_USE,
00205 SURROGATE,
00206 CONNECTOR_PUNCTUATION,
00207 DASH_PUNCTUATION,
00208 OPEN_PUNCTUATION,
00209 CLOSE_PUNCTUATION,
00210 INITIAL_QUOTE_PUNCTUATION,
00211 FINAL_QUOTE_PUNCTUATION,
00212 OTHER_PUNCTUATION,
00213 MATH_SYMBOL,
00214 CURRENCY_SYMBOL,
00215 MODIFIER_SYMBOL,
00216 OTHER_SYMBOL
00217 } category;
00218
00219 namespace Internal {
00225 XAPIAN_VISIBILITY_DEFAULT
00226 int get_character_info(unsigned ch);
00227
00231 inline int get_case_type(int info) { return ((info & 0xe0) >> 5); }
00232
00234 inline category get_category(int info) { return static_cast<category>(info & 0x1f); }
00235
00239 inline int get_delta(int info) {
00240
00241
00242
00243
00244
00245 return (info >= 0) ? (info >> 15) : (~(~info >> 15));
00246 }
00247 }
00248
00258 XAPIAN_VISIBILITY_DEFAULT
00259 unsigned nonascii_to_utf8(unsigned ch, char * buf);
00260
00268 inline unsigned to_utf8(unsigned ch, char *buf) {
00269 if (ch < 128) {
00270 *buf = static_cast<unsigned char>(ch);
00271 return 1;
00272 }
00273 return Xapian::Unicode::nonascii_to_utf8(ch, buf);
00274 }
00275
00279 inline void append_utf8(std::string &s, unsigned ch) {
00280 char buf[4];
00281 s.append(buf, to_utf8(ch, buf));
00282 }
00283
00285 inline category get_category(unsigned ch) {
00286
00287 if (ch >= 0x110000) return Xapian::Unicode::UNASSIGNED;
00288 return Internal::get_category(Internal::get_character_info(ch));
00289 }
00290
00292 inline bool is_wordchar(unsigned ch) {
00293 const unsigned int WORDCHAR_MASK =
00294 (1 << Xapian::Unicode::UPPERCASE_LETTER) |
00295 (1 << Xapian::Unicode::LOWERCASE_LETTER) |
00296 (1 << Xapian::Unicode::TITLECASE_LETTER) |
00297 (1 << Xapian::Unicode::MODIFIER_LETTER) |
00298 (1 << Xapian::Unicode::OTHER_LETTER) |
00299 (1 << Xapian::Unicode::DECIMAL_DIGIT_NUMBER) |
00300 (1 << Xapian::Unicode::LETTER_NUMBER) |
00301 (1 << Xapian::Unicode::OTHER_NUMBER) |
00302 (1 << Xapian::Unicode::CONNECTOR_PUNCTUATION);
00303 return ((WORDCHAR_MASK >> get_category(ch)) & 1);
00304 }
00305
00307 inline bool is_whitespace(unsigned ch) {
00308 const unsigned int WHITESPACE_MASK =
00309 (1 << Xapian::Unicode::CONTROL) |
00310 (1 << Xapian::Unicode::SPACE_SEPARATOR) |
00311 (1 << Xapian::Unicode::LINE_SEPARATOR) |
00312 (1 << Xapian::Unicode::PARAGRAPH_SEPARATOR);
00313 return ((WHITESPACE_MASK >> get_category(ch)) & 1);
00314 }
00315
00317 inline bool is_currency(unsigned ch) {
00318 return (get_category(ch) == Xapian::Unicode::CURRENCY_SYMBOL);
00319 }
00320
00322 inline unsigned tolower(unsigned ch) {
00323 int info;
00324
00325 if (ch >= 0x110000 || !(Internal::get_case_type((info = Xapian::Unicode::Internal::get_character_info(ch))) & 2))
00326 return ch;
00327 return ch + Internal::get_delta(info);
00328 }
00329
00331 inline unsigned toupper(unsigned ch) {
00332 int info;
00333
00334 if (ch >= 0x110000 || !(Internal::get_case_type((info = Xapian::Unicode::Internal::get_character_info(ch))) & 4))
00335 return ch;
00336 return ch - Internal::get_delta(info);
00337 }
00338
00340 inline std::string
00341 tolower(const std::string &term)
00342 {
00343 std::string result;
00344 result.reserve(term.size());
00345 for (Utf8Iterator i(term); i != Utf8Iterator(); ++i) {
00346 append_utf8(result, tolower(*i));
00347 }
00348 return result;
00349 }
00350
00352 inline std::string
00353 toupper(const std::string &term)
00354 {
00355 std::string result;
00356 result.reserve(term.size());
00357 for (Utf8Iterator i(term); i != Utf8Iterator(); ++i) {
00358 append_utf8(result, toupper(*i));
00359 }
00360 return result;
00361 }
00362
00363 }
00364
00365 }
00366
00367 #endif // XAPIAN_INCLUDED_UNICODE_H