~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/utf8/utf8.h

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
   vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 
 
4
  Copyright (C) 2010 Monty Taylor
 
5
 
 
6
  This program is free software; you can redistribute it and/or modify
 
7
  it under the terms of the GNU General Public License as published by
 
8
  the Free Software Foundation; version 2 of the License.
 
9
 
 
10
  This program is distributed in the hope that it will be useful,
 
11
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
  GNU General Public License for more details.
 
14
 
 
15
  You should have received a copy of the GNU General Public License
 
16
  along with this program; if not, write to the Free Software
 
17
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 
 
19
  Copyright (c) 1995-2006 International Business Machines Corporation and others
 
20
 
 
21
  All rights reserved.
 
22
 
 
23
  Permission is hereby granted, free of charge, to any person obtaining a copy
 
24
  of this software and associated documentation files (the "Software"),
 
25
  to deal in the Software without restriction, including without limitation
 
26
  the rights to use, copy, modify, merge, publish, distribute, and/or sell
 
27
  copies of the Software, and to permit persons
 
28
  to whom the Software is furnished to do so, provided that the above
 
29
  copyright notice(s) and this permission notice appear in all copies
 
30
  of the Software and that both the above copyright notice(s) and this
 
31
  permission notice appear in supporting documentation.
 
32
 
 
33
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
34
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
35
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
 
36
  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE
 
37
  LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR
 
38
  ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 
39
  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 
40
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
41
 
 
42
  Except as contained in this notice, the name of a copyright holder shall not
 
43
  be used in advertising or otherwise to promote the sale, use or other dealings
 
44
  in this Software without prior written authorization of the copyright holder.
 
45
*/
 
46
 
 
47
#ifndef DRIZZLED_UTF8_UTF8_H
 
48
#define DRIZZLED_UTF8_UTF8_H
 
49
 
 
50
#include <string>
 
51
 
 
52
namespace drizzled
 
53
{
 
54
namespace utf8
 
55
{
 
56
 
 
57
/**
 
58
 * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
 
59
 */
 
60
static const int MAX_LENGTH= 4;
 
61
 
 
62
/**
 
63
 * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
 
64
 * @param c 8-bit code unit (byte)
 
65
 * @return TRUE or FALSE
 
66
 */
 
67
template <class T>
 
68
bool is_single(T c)
 
69
{
 
70
  return (static_cast<uint8_t>(c) & 0x80) == 0;
 
71
}
 
72
 
 
73
/**
 
74
 * How many code units (bytes) are used for the UTF-8 encoding
 
75
 * of this Unicode code point?
 
76
 * @param c 32-bit code point
 
77
 * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
 
78
 */
 
79
static inline int codepoint_length(uint32_t c)
 
80
{
 
81
  return (c <= 0x7f ? 1 :
 
82
          (c <= 0x7ff ? 2 :
 
83
           (c <= 0xd7ff ? 3 :
 
84
            (c <= 0xdfff || c>0x10ffff ? 0 :
 
85
             (c <= 0xffff ? 3 : 4)))));
 
86
}
 
87
 
 
88
/**
 
89
 * How many bytes are used for the UTF-8 encoding of the codepoint of which
 
90
 * this is the first byte?
 
91
 * @param c 8-bit leading byte
 
92
 * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
 
93
 */ 
 
94
template <class T>
 
95
int sequence_length(T c)
 
96
{
 
97
  return (static_cast<uint8_t>(c) < 0x80 ? 1 :
 
98
          ((static_cast<uint8_t>(c) >> 5) == 0x6 ? 2 :
 
99
           ((static_cast<uint8_t>(c) >> 4) == 0xe ? 3 :
 
100
            ((static_cast<uint8_t>(c) >> 3) == 0x1e ? 4 : 0))));
 
101
}
 
102
 
 
103
 
 
104
/**
 
105
 * How many logical characters does the given UTF-8 string occupy? Useful when
 
106
 * needing to calculate char length rather than byte length of a string
 
107
 * @param in_string string to measure
 
108
 * @return length in characters of given string
 
109
 */
 
110
static inline uint32_t char_length(const std::string &in_string)
 
111
{
 
112
  uint32_t length= 0;
 
113
  std::string::const_iterator iter= in_string.begin();
 
114
  while (iter < in_string.end())
 
115
  {
 
116
    length++;
 
117
    iter += sequence_length(*iter);
 
118
  }
 
119
  return length;
 
120
}
 
121
 
 
122
/**
 
123
 * How many logical characters does the given UTF-8 string occupy? Useful when
 
124
 * needing to calculate char length rather than byte length of a string
 
125
 * @param in_string string to measure
 
126
 * @return length in characters of given string
 
127
 */
 
128
static inline uint32_t char_length(const char *in_string)
 
129
{
 
130
  const std::string process_string(in_string);
 
131
  return char_length(process_string);
 
132
}
 
133
 
 
134
 
 
135
} /* namespace utf8 */
 
136
} /* namespace drizzled */
 
137
 
 
138
#endif /* DRIZZLED_UTF8_UTF8_H */