include/xapian/unicode.h

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2006,2007 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 // FIXME: Nail down API details (what to expose; what names to use).
00022 // FIXME: Add missing documentation comments.
00023 
00024 #ifndef XAPIAN_INCLUDED_UNICODE_H
00025 #define XAPIAN_INCLUDED_UNICODE_H
00026 
00027 #include <xapian/visibility.h>
00028 
00029 #include <string>
00030 
00031 namespace Xapian {
00032 
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:
00046     const char * raw() const { return reinterpret_cast<const char *>(p ? p : end); }
00047     size_t left() const { return p ? end - p : 0; }
00048 
00049     void assign(const char *p_, size_t len) {
00050         if (len) {
00051             p = reinterpret_cast<const unsigned char*>(p_);
00052             end = p + len;
00053             seqlen = 0;
00054         } else {
00055             p = NULL;
00056         }
00057     }
00058 
00059     Utf8Iterator(const char *p_);
00060 
00061     Utf8Iterator(const char *p_, size_t len) { assign(p_, len); }
00062 
00063     Utf8Iterator(const std::string &s) { assign(s.data(), s.size()); }
00064 
00065     Utf8Iterator() : p(NULL), end(0), seqlen(0) { }
00066 
00067     unsigned operator*() const;
00068 
00069     Utf8Iterator operator++(int) {
00070         // If we've not calculated seqlen yet, do so.
00071         if (seqlen == 0) calculate_sequence_length();
00072         const unsigned char *old_p = p;
00073         unsigned old_seqlen = seqlen;
00074         p += seqlen;
00075         if (p == end) p = NULL;
00076         seqlen = 0;
00077         return Utf8Iterator(old_p, end, old_seqlen);
00078     }
00079 
00080     Utf8Iterator & operator++() {
00081         this->operator++(0);
00082         return *this;
00083     }
00084 
00085     bool operator==(const Utf8Iterator &other) const { return p == other.p; }
00086 
00087     bool operator!=(const Utf8Iterator &other) const { return p != other.p; }
00088 
00090 
00091     typedef std::input_iterator_tag iterator_category;
00092     typedef unsigned value_type;
00093     typedef size_t difference_type;
00094     typedef const unsigned * pointer;
00095     typedef const unsigned & reference;
00097 };
00098 
00099 namespace Unicode {
00100 
00102 typedef enum {
00103     UNASSIGNED,
00104     UPPERCASE_LETTER,
00105     LOWERCASE_LETTER,
00106     TITLECASE_LETTER,
00107     MODIFIER_LETTER,
00108     OTHER_LETTER,
00109     NON_SPACING_MARK,
00110     ENCLOSING_MARK,
00111     COMBINING_SPACING_MARK,
00112     DECIMAL_DIGIT_NUMBER,
00113     LETTER_NUMBER,
00114     OTHER_NUMBER,
00115     SPACE_SEPARATOR,
00116     LINE_SEPARATOR,
00117     PARAGRAPH_SEPARATOR,
00118     CONTROL,
00119     FORMAT,
00120     PRIVATE_USE,
00121     SURROGATE,
00122     CONNECTOR_PUNCTUATION,
00123     DASH_PUNCTUATION,
00124     OPEN_PUNCTUATION,
00125     CLOSE_PUNCTUATION,
00126     INITIAL_QUOTE_PUNCTUATION,
00127     FINAL_QUOTE_PUNCTUATION,
00128     OTHER_PUNCTUATION,
00129     MATH_SYMBOL,
00130     CURRENCY_SYMBOL,
00131     MODIFIER_SYMBOL,
00132     OTHER_SYMBOL
00133 } category;
00134 
00135 namespace Internal {
00136     /* Extract the information about a character from the Unicode character
00137      * tables.
00138      *
00139      * ch must be < 0x10000
00140      */
00141     XAPIAN_VISIBILITY_DEFAULT
00142     int get_character_info(unsigned ch);
00143 
00145     inline int get_case_type(int info) { return ((info & 0xe0) >> 5); }
00146 
00148     inline category get_category(int info) { return static_cast<category>(info & 0x1f); }
00149 
00151     inline int get_delta(int info) {
00152         /* It's implementation defined if sign extension happens on right shift of
00153          * a signed int, hence the conditional (hopefully the compiler will spot
00154          * this and optimise it to a sign-extending shift on architectures with
00155          * a suitable instruction).
00156          */
00157         return (info >= 0) ? (info >> 22) : (~(~info >> 22));
00158     }
00159 }
00160 
00161 // buf must have space for (at least) 4 bytes.
00162 XAPIAN_VISIBILITY_DEFAULT
00163 unsigned nonascii_to_utf8(unsigned ch, char * buf);
00164 
00165 // buf must have space for at least 4 bytes.
00166 inline unsigned to_utf8(unsigned ch, char *buf) {
00167     if (ch < 128) {
00168         *buf = static_cast<unsigned char>(ch);
00169         return 1;
00170     }
00171     return Xapian::Unicode::nonascii_to_utf8(ch, buf);
00172 }
00173 
00174 inline category get_category(unsigned ch) {
00175     return Internal::get_category(Internal::get_character_info(ch));
00176 }
00177 
00178 inline bool is_wordchar(unsigned ch) {
00179     const unsigned int WORDCHAR_MASK =
00180             (1 << Xapian::Unicode::UPPERCASE_LETTER) |
00181             (1 << Xapian::Unicode::LOWERCASE_LETTER) |
00182             (1 << Xapian::Unicode::TITLECASE_LETTER) |
00183             (1 << Xapian::Unicode::MODIFIER_LETTER) |
00184             (1 << Xapian::Unicode::OTHER_LETTER) |
00185             (1 << Xapian::Unicode::DECIMAL_DIGIT_NUMBER) |
00186             (1 << Xapian::Unicode::LETTER_NUMBER) |
00187             (1 << Xapian::Unicode::OTHER_NUMBER);
00188     // The TCL Unicode routines only support the BMP.  For now, just assume
00189     // all characters outside the BMP are word characters.
00190     return (ch >= 0x10000) || ((WORDCHAR_MASK >> get_category(ch)) & 1);
00191 }
00192 
00193 inline bool is_currency(unsigned ch) {
00194     // The TCL Unicode routines only support the BMP.  For now, just assume
00195     // no characters outside the BMP are currency characters.
00196     return (ch < 0x10000) && (get_category(ch) == Xapian::Unicode::CURRENCY_SYMBOL);
00197 }
00198 
00199 inline unsigned tolower(unsigned ch) {
00200     int info;
00201     // The TCL Unicode routines only support the BMP.  For now, just assume
00202     // all characters outside the BMP can't have their case converted.
00203     if (ch >= 0x10000 || !(Internal::get_case_type((info = Xapian::Unicode::Internal::get_character_info(ch))) & 2))
00204         return ch;
00205     return ch + Internal::get_delta(info);
00206 }
00207 
00208 inline void append_utf8(std::string &s, unsigned ch) {
00209     char buf[4];
00210     s.append(buf, to_utf8(ch, buf));
00211 }
00212 
00213 inline std::string
00214 tolower(const std::string &term)
00215 {
00216     std::string result;
00217     result.reserve(term.size());
00218     for (Utf8Iterator i(term); i != Utf8Iterator(); ++i) {
00219         append_utf8(result, tolower(*i));
00220     }
00221     return result;
00222 }
00223 
00224 }
00225 
00226 }
00227 
00228 #endif // XAPIAN_INCLUDED_UNICODE_H

Documentation for Xapian (version 0.9.10_svn8391).
Generated on 26 Apr 2007 by Doxygen 1.4.6.