~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mystrings/utf8.h

  • Committer: Stewart Smith
  • Date: 2009-10-19 03:27:43 UTC
  • mto: This revision was merged to the branch mainline in revision 1188.
  • Revision ID: stewart@flamingspork.com-20091019032743-xajbflq0ktpe9vw2
remove --disable-warnings in func_in_null_scan.test

Show diffs side-by-side

added added

removed removed

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