tesseract  4.0.0-1-g2a2b
tesseract::UNICHAR Class Reference

#include <unichar.h>

Classes

class  const_iterator
 

Public Member Functions

 UNICHAR ()
 
 UNICHAR (const char *utf8_str, int len)
 
 UNICHAR (int unicode)
 
int first_uni () const
 
int utf8_len () const
 
const char * utf8 () const
 
char * utf8_str () const
 

Static Public Member Functions

static int utf8_step (const char *utf8_str)
 
static const_iterator begin (const char *utf8_str, const int byte_length)
 
static const_iterator end (const char *utf8_str, const int byte_length)
 
static std::vector< char32UTF8ToUTF32 (const char *utf8_str)
 
static std::string UTF32ToUTF8 (const std::vector< char32 > &str32)
 

Detailed Description

Definition at line 58 of file unichar.h.

Constructor & Destructor Documentation

◆ UNICHAR() [1/3]

tesseract::UNICHAR::UNICHAR ( )
inline

Definition at line 60 of file unichar.h.

60  {
61  memset(chars, 0, UNICHAR_LEN);
62  }
#define UNICHAR_LEN
Definition: unichar.h:31

◆ UNICHAR() [2/3]

tesseract::UNICHAR::UNICHAR ( const char *  utf8_str,
int  len 
)

Definition at line 33 of file unichar.cpp.

33  {
34  int total_len = 0;
35  int step = 0;
36  if (len < 0) {
37  for (len = 0; len < UNICHAR_LEN && utf8_str[len] != 0; ++len);
38  }
39  for (total_len = 0; total_len < len; total_len += step) {
40  step = utf8_step(utf8_str + total_len);
41  if (total_len + step > UNICHAR_LEN)
42  break; // Too long.
43  if (step == 0)
44  break; // Illegal first byte.
45  int i;
46  for (i = 1; i < step; ++i)
47  if ((utf8_str[total_len + i] & 0xc0) != 0x80)
48  break;
49  if (i < step)
50  break; // Illegal surrogate
51  }
52  memcpy(chars, utf8_str, total_len);
53  if (total_len < UNICHAR_LEN) {
54  chars[UNICHAR_LEN - 1] = total_len;
55  while (total_len < UNICHAR_LEN - 1)
56  chars[total_len++] = 0;
57  }
58 }
#define UNICHAR_LEN
Definition: unichar.h:31
char * utf8_str() const
Definition: unichar.cpp:127
static int utf8_step(const char *utf8_str)
Definition: unichar.cpp:136

◆ UNICHAR() [3/3]

tesseract::UNICHAR::UNICHAR ( int  unicode)
explicit

Definition at line 62 of file unichar.cpp.

62  {
63  const int bytemask = 0xBF;
64  const int bytemark = 0x80;
65 
66  if (unicode < 0x80) {
67  chars[UNICHAR_LEN - 1] = 1;
68  chars[2] = 0;
69  chars[1] = 0;
70  chars[0] = static_cast<char>(unicode);
71  } else if (unicode < 0x800) {
72  chars[UNICHAR_LEN - 1] = 2;
73  chars[2] = 0;
74  chars[1] = static_cast<char>((unicode | bytemark) & bytemask);
75  unicode >>= 6;
76  chars[0] = static_cast<char>(unicode | 0xc0);
77  } else if (unicode < 0x10000) {
78  chars[UNICHAR_LEN - 1] = 3;
79  chars[2] = static_cast<char>((unicode | bytemark) & bytemask);
80  unicode >>= 6;
81  chars[1] = static_cast<char>((unicode | bytemark) & bytemask);
82  unicode >>= 6;
83  chars[0] = static_cast<char>(unicode | 0xe0);
84  } else if (unicode <= UNI_MAX_LEGAL_UTF32) {
85  chars[UNICHAR_LEN - 1] = 4;
86  chars[3] = static_cast<char>((unicode | bytemark) & bytemask);
87  unicode >>= 6;
88  chars[2] = static_cast<char>((unicode | bytemark) & bytemask);
89  unicode >>= 6;
90  chars[1] = static_cast<char>((unicode | bytemark) & bytemask);
91  unicode >>= 6;
92  chars[0] = static_cast<char>(unicode | 0xf0);
93  } else {
94  memset(chars, 0, UNICHAR_LEN);
95  }
96 }
#define UNICHAR_LEN
Definition: unichar.h:31
#define UNI_MAX_LEGAL_UTF32
Definition: unichar.cpp:25

Member Function Documentation

◆ begin()

UNICHAR::const_iterator tesseract::UNICHAR::begin ( const char *  utf8_str,
const int  byte_length 
)
static

Definition at line 202 of file unichar.cpp.

202  {
203  return UNICHAR::const_iterator(utf8_str);
204 }
char * utf8_str() const
Definition: unichar.cpp:127

◆ end()

UNICHAR::const_iterator tesseract::UNICHAR::end ( const char *  utf8_str,
const int  byte_length 
)
static

Definition at line 206 of file unichar.cpp.

206  {
207  return UNICHAR::const_iterator(utf8_str + len);
208 }
char * utf8_str() const
Definition: unichar.cpp:127

◆ first_uni()

int tesseract::UNICHAR::first_uni ( ) const

Definition at line 99 of file unichar.cpp.

99  {
100  static const int utf8_offsets[5] = {
101  0, 0, 0x3080, 0xE2080, 0x3C82080
102  };
103  int uni = 0;
104  int len = utf8_step(chars);
105  const char* src = chars;
106 
107  switch (len) {
108  default:
109  break;
110  case 4:
111  uni += static_cast<unsigned char>(*src++);
112  uni <<= 6;
113  case 3:
114  uni += static_cast<unsigned char>(*src++);
115  uni <<= 6;
116  case 2:
117  uni += static_cast<unsigned char>(*src++);
118  uni <<= 6;
119  case 1:
120  uni += static_cast<unsigned char>(*src++);
121  }
122  uni -= utf8_offsets[len];
123  return uni;
124 }
static int utf8_step(const char *utf8_str)
Definition: unichar.cpp:136

◆ UTF32ToUTF8()

std::string tesseract::UNICHAR::UTF32ToUTF8 ( const std::vector< char32 > &  str32)
static

Definition at line 230 of file unichar.cpp.

230  {
231  std::string utf8_str;
232  for (char32 ch : str32) {
233  UNICHAR uni_ch(ch);
234  int step;
235  if (uni_ch.utf8_len() > 0 && (step = utf8_step(uni_ch.utf8())) > 0) {
236  utf8_str.append(uni_ch.utf8(), step);
237  } else {
238  return "";
239  }
240  }
241  return utf8_str;
242 }
signed int char32
char * utf8_str() const
Definition: unichar.cpp:127
static int utf8_step(const char *utf8_str)
Definition: unichar.cpp:136

◆ utf8()

const char* tesseract::UNICHAR::utf8 ( ) const
inline

Definition at line 84 of file unichar.h.

84  {
85  return chars;
86  }

◆ utf8_len()

int tesseract::UNICHAR::utf8_len ( ) const
inline

Definition at line 78 of file unichar.h.

78  {
79  int len = chars[UNICHAR_LEN - 1];
80  return len >=0 && len < UNICHAR_LEN ? len : UNICHAR_LEN;
81  }
#define UNICHAR_LEN
Definition: unichar.h:31

◆ utf8_step()

int tesseract::UNICHAR::utf8_step ( const char *  utf8_str)
static

Definition at line 136 of file unichar.cpp.

136  {
137  static const char utf8_bytes[256] = {
138  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
139  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
140  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
141  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
142  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
143  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
144  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
145  3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0
146  };
147 
148  return utf8_bytes[static_cast<unsigned char>(*utf8_str)];
149 }

◆ utf8_str()

char * tesseract::UNICHAR::utf8_str ( ) const

Definition at line 127 of file unichar.cpp.

127  {
128  int len = utf8_len();
129  char* str = new char[len + 1];
130  memcpy(str, chars, len);
131  str[len] = 0;
132  return str;
133 }
int utf8_len() const
Definition: unichar.h:78

◆ UTF8ToUTF32()

std::vector< char32 > tesseract::UNICHAR::UTF8ToUTF32 ( const char *  utf8_str)
static

Definition at line 213 of file unichar.cpp.

213  {
214  const int utf8_length = strlen(utf8_str);
215  std::vector<char32> unicodes;
216  unicodes.reserve(utf8_length);
217  const_iterator end_it(end(utf8_str, utf8_length));
218  for (const_iterator it(begin(utf8_str, utf8_length)); it != end_it; ++it) {
219  if (it.is_legal()) {
220  unicodes.push_back(*it);
221  } else {
222  unicodes.clear();
223  return unicodes;
224  }
225  }
226  return unicodes;
227 }
static const_iterator begin(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:202
char * utf8_str() const
Definition: unichar.cpp:127
static const_iterator end(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:206

The documentation for this class was generated from the following files: