Doxygen
Loading...
Searching...
No Matches
utf8.cpp File Reference
#include <cstdint>
#include <sstream>
#include "utf8.h"
#include "caseconvert.h"
#include "textstream.h"
#include "portable.h"
Include dependency graph for utf8.cpp:

Go to the source code of this file.

Functions

uint8_t getUTF8CharNumBytes (char c)
 Returns the number of bytes making up a single UTF8 character given the first byte in the sequence.
static uint32_t decode_utf8 (const char *data, int numBytes) noexcept
static uint32_t convertUTF8CharToUnicode (const char *s, size_t bytesLeft, int &len)
std::string getUTF8CharAt (const std::string &input, size_t pos)
 Returns the UTF8 character found at byte position pos in the input string.
uint32_t getUnicodeForUTF8CharAt (const std::string &input, size_t pos)
 Returns the 32bit Unicode value matching character at byte position pos in the UTF8 encoded input.
static char asciiToLower (uint32_t code)
static char asciiToUpper (uint32_t code)
static std::string caseConvert (const std::string &input, char(*asciiConversionFunc)(uint32_t code), const char *(*conversionFunc)(uint32_t code))
std::string convertUTF8ToLower (const std::string &input)
 Converts the input string into a lower case version, also taking into account non-ASCII characters that has a lower case variant.
std::string convertUTF8ToUpper (const std::string &input)
 Converts the input string into a upper case version, also taking into account non-ASCII characters that has a upper case variant.
const char * writeUTF8Char (TextStream &t, const char *s)
 Writes the UTF8 character pointed to by s to stream t and returns a pointer to the next character.
bool lastUTF8CharIsMultibyte (const std::string &input)
 Returns true iff the last character in input is a multibyte character.
bool isUTF8CharUpperCase (const std::string &input, size_t pos)
 Returns true iff the input string at byte position pos holds an upper case character.
int isUTF8NonBreakableSpace (const char *input)
 Check if the first character pointed at by input is a non-breakable whitespace character.
bool isUTF8PunctuationCharacter (uint32_t unicode)
 Check if the given Unicode character represents a punctuation character.
bool transcodeCharacterStringToUTF8 (std::string &input, const char *inputEncoding)
 Recodes the input string from the given input encoding to UTF8.

Function Documentation

◆ asciiToLower()

char asciiToLower ( uint32_t code)
inlinestatic

Definition at line 143 of file utf8.cpp.

144{
145 return code>='A' && code<='Z' ? static_cast<char>(code+'a'-'A') : static_cast<char>(code);
146}

Referenced by convertUTF8ToLower().

◆ asciiToUpper()

char asciiToUpper ( uint32_t code)
inlinestatic

Definition at line 148 of file utf8.cpp.

149{
150 return code>='a' && code<='z' ? static_cast<char>(code+'A'-'a') : static_cast<char>(code);
151}

Referenced by convertUTF8ToUpper().

◆ caseConvert()

std::string caseConvert ( const std::string & input,
char(* asciiConversionFunc )(uint32_t code),
const char * conversionFunc (*)(uint32_t code) )
inlinestatic

Definition at line 153 of file utf8.cpp.

156{
157 uint32_t code=0;
158 std::string result;
159 result.reserve(input.length()); // assume all ASCII characters
160 int len=0;
161 size_t bytesLeft = input.length();
162 const char *p = input.c_str();
163 while ((code=convertUTF8CharToUnicode(p,bytesLeft,len)))
164 {
165 if (code<128) // ASCII case
166 {
167 char c = asciiConversionFunc(code);
168 result+=c;
169 }
170 else // generic case
171 {
172 const char *conv = conversionFunc(code);
173 if (conv==nullptr) // no difference between lower and upper case
174 {
175 result.append(p,len);
176 }
177 else // replace the input character with the conversion result
178 {
179 result.append(conv);
180 }
181 }
182 p+=len;
183 bytesLeft-=len;
184 }
185 return result;
186}
static uint32_t convertUTF8CharToUnicode(const char *s, size_t bytesLeft, int &len)
Definition utf8.cpp:70

References convertUTF8CharToUnicode().

Referenced by convertUTF8ToLower(), and convertUTF8ToUpper().

◆ convertUTF8CharToUnicode()

uint32_t convertUTF8CharToUnicode ( const char * s,
size_t bytesLeft,
int & len )
inlinestatic

Definition at line 70 of file utf8.cpp.

71{
72 if (s==nullptr || bytesLeft==0)
73 {
74 len=0;
75 return 0;
76 }
77 unsigned char uc = static_cast<unsigned char>(*s);
78 if (uc<128) // ASCII case
79 {
80 len=1;
81 return uc;
82 }
83 switch (bytesLeft)
84 {
85 default:
86 if ((uc&0xFEu)==0xFCu)// 1111110X six bytes
87 {
88 len=6;
89 return decode_utf8(s,len);
90 }
91 // fall through
92 case 5:
93 if ((uc&0xFCu)==0xF8u) // 111110XX five bytes
94 {
95 len=5;
96 return decode_utf8(s,len);
97 }
98 // fall through
99 case 4:
100 if ((uc&0xF8u)==0xF0u) // 11110XXX four bytes
101 {
102 len=4;
103 return decode_utf8(s,len);
104 }
105 // fall through
106 case 3:
107 if ((uc&0xF0u)==0xE0u) // 1110XXXX three bytes
108 {
109 len=3;
110 return decode_utf8(s,len);
111 }
112 // fall through
113 case 2:
114 if ((uc&0xE0u)==0xC0u) // 110XXXXX two bytes
115 {
116 len=2;
117 return decode_utf8(s,len);
118 }
119 // fall through
120 case 1:
121 {
122 len=1;
123 return uc;
124 }
125 }
126}
static uint32_t decode_utf8(const char *data, int numBytes) noexcept
Definition utf8.cpp:56

References decode_utf8().

Referenced by caseConvert(), getUnicodeForUTF8CharAt(), and isUTF8CharUpperCase().

◆ convertUTF8ToLower()

std::string convertUTF8ToLower ( const std::string & input)

Converts the input string into a lower case version, also taking into account non-ASCII characters that has a lower case variant.

Definition at line 188 of file utf8.cpp.

189{
191}
const char * convertUnicodeToLower(uint32_t code)
static char asciiToLower(uint32_t code)
Definition utf8.cpp:143
static std::string caseConvert(const std::string &input, char(*asciiConversionFunc)(uint32_t code), const char *(*conversionFunc)(uint32_t code))
Definition utf8.cpp:153

References asciiToLower(), caseConvert(), and convertUnicodeToLower().

Referenced by SearchIndexInfo::add(), Index::addClassMemberNameToIndex(), Index::addFileMemberNameToIndex(), Index::addModuleMemberNameToIndex(), Index::addNamespaceMemberNameToIndex(), AnchorGenerator::generate(), DString::lower(), FileNameFn::searchKey(), SearchTerm::termEncoded(), and HtmlGenerator::writeLabel().

◆ convertUTF8ToUpper()

std::string convertUTF8ToUpper ( const std::string & input)

Converts the input string into a upper case version, also taking into account non-ASCII characters that has a upper case variant.

Definition at line 193 of file utf8.cpp.

194{
196}
const char * convertUnicodeToUpper(uint32_t code)
Definition caseconvert.h:12
static char asciiToUpper(uint32_t code)
Definition utf8.cpp:148

References asciiToUpper(), caseConvert(), and convertUnicodeToUpper().

Referenced by Translator::createNoun(), DString::upper(), and writeAlphabeticalClassList().

◆ decode_utf8()

uint32_t decode_utf8 ( const char * data,
int numBytes )
inlinestaticnoexcept

Decodes a given input of utf8 data to a unicode code point given the number of bytes it's made of

Definition at line 56 of file utf8.cpp.

57{
58 uint32_t cp = static_cast<unsigned char>(*data);
59 if (numBytes>1)
60 {
61 cp &= 0x7F >> numBytes; // Mask out the header bits
62 for (int i=1 ; i<numBytes ; i++)
63 {
64 cp = (cp<<6) | (static_cast<unsigned char>(data[i])&0x3F);
65 }
66 }
67 return cp;
68}

Referenced by convertUTF8CharToUnicode().

◆ getUnicodeForUTF8CharAt()

uint32_t getUnicodeForUTF8CharAt ( const std::string & input,
size_t pos )

Returns the 32bit Unicode value matching character at byte position pos in the UTF8 encoded input.

Definition at line 136 of file utf8.cpp.

137{
138 std::string charS = getUTF8CharAt(input,pos);
139 int len=0;
140 return convertUTF8CharToUnicode(charS.c_str(),charS.length(),len);
141}
std::string getUTF8CharAt(const std::string &input, size_t pos)
Returns the UTF8 character found at byte position pos in the input string.
Definition utf8.cpp:128

References convertUTF8CharToUnicode(), and getUTF8CharAt().

Referenced by AnchorGenerator::generate().

◆ getUTF8CharAt()

std::string getUTF8CharAt ( const std::string & input,
size_t pos )

Returns the UTF8 character found at byte position pos in the input string.

The resulting string can be a multi byte sequence.

Definition at line 128 of file utf8.cpp.

129{
130 if (input.length()<=pos) return std::string();
131 int numBytes=getUTF8CharNumBytes(input[pos]);
132 if (input.length()<pos+numBytes) return std::string();
133 return input.substr(pos,numBytes);
134}
uint8_t getUTF8CharNumBytes(char c)
Returns the number of bytes making up a single UTF8 character given the first byte in the sequence.
Definition utf8.cpp:24

References getUTF8CharNumBytes().

Referenced by SearchIndexInfo::add(), Index::addClassMemberNameToIndex(), Index::addFileMemberNameToIndex(), Index::addModuleMemberNameToIndex(), Index::addNamespaceMemberNameToIndex(), Translator::createNoun(), AnchorGenerator::generate(), getUnicodeForUTF8CharAt(), and writeAlphabeticalClassList().

◆ getUTF8CharNumBytes()

uint8_t getUTF8CharNumBytes ( char c)

Returns the number of bytes making up a single UTF8 character given the first byte in the sequence.

Definition at line 24 of file utf8.cpp.

25{
26 uint8_t num=1;
27 unsigned char uc = static_cast<unsigned char>(c);
28 if (uc>=0x80u) // multibyte character
29 {
30 if ((uc&0xE0u)==0xC0u)
31 {
32 num=2; // 110x.xxxx: 2 byte character
33 }
34 if ((uc&0xF0u)==0xE0u)
35 {
36 num=3; // 1110.xxxx: 3 byte character
37 }
38 if ((uc&0xF8u)==0xF0u)
39 {
40 num=4; // 1111.0xxx: 4 byte character
41 }
42 if ((uc&0xFCu)==0xF8u)
43 {
44 num=5; // 1111.10xx: 5 byte character
45 }
46 if ((uc&0xFEu)==0xFCu)
47 {
48 num=6; // 1111.110x: 6 byte character
49 }
50 }
51 return num;
52}

Referenced by detab(), escapeCharsInString(), AnchorGenerator::generate(), getUTF8CharAt(), nextUTF8CharPosition(), updateColumnCount(), and writeUTF8Char().

◆ isUTF8CharUpperCase()

bool isUTF8CharUpperCase ( const std::string & input,
size_t pos )

Returns true iff the input string at byte position pos holds an upper case character.

Definition at line 219 of file utf8.cpp.

220{
221 if (input.length()<=pos) return false;
222 int len=0;
223 // turn the UTF8 character at position pos into a unicode value
224 uint32_t code = convertUTF8CharToUnicode(input.c_str()+pos,input.length()-pos,len);
225 // check if the character can be converted to lower case, if so it was an upper case character
226 return convertUnicodeToLower(code)!=nullptr;
227}

References convertUnicodeToLower(), and convertUTF8CharToUnicode().

Referenced by DefinitionImpl::_setBriefDescription().

◆ isUTF8NonBreakableSpace()

int isUTF8NonBreakableSpace ( const char * input)

Check if the first character pointed at by input is a non-breakable whitespace character.

Returns the byte size of the character if there is match or 0 if not.

Definition at line 229 of file utf8.cpp.

230{
231 return (static_cast<unsigned char>(input[0])==0xC2 &&
232 static_cast<unsigned char>(input[1])==0xA0) ? 2 : 0;
233}

Referenced by detab().

◆ isUTF8PunctuationCharacter()

bool isUTF8PunctuationCharacter ( uint32_t unicode)

Check if the given Unicode character represents a punctuation character.

Definition at line 235 of file utf8.cpp.

236{
237 bool b = isPunctuationCharacter(unicode);
238 return b;
239}
bool isPunctuationCharacter(uint32_t code)

References isPunctuationCharacter().

Referenced by AnchorGenerator::generate().

◆ lastUTF8CharIsMultibyte()

bool lastUTF8CharIsMultibyte ( const std::string & input)

Returns true iff the last character in input is a multibyte character.

Definition at line 213 of file utf8.cpp.

214{
215 // last byte is part of a multibyte UTF8 char if bit 8 is set and bit 7 is not
216 return !input.empty() && (static_cast<unsigned char>(input[input.length()-1])&0xC0)==0x80;
217}

Referenced by DefinitionImpl::_setBriefDescription().

◆ transcodeCharacterStringToUTF8()

bool transcodeCharacterStringToUTF8 ( std::string & input,
const char * inputEncoding )

Recodes the input string from the given input encoding to UTF8.

Returns true if successful, false otherwise.

Definition at line 241 of file utf8.cpp.

242{
243 const char *outputEncoding = "UTF-8";
244 if (inputEncoding==nullptr || dstricmp(inputEncoding,outputEncoding)==0) return true;
245 size_t inputSize=input.length();
246 size_t outputSize=inputSize*4;
247 DString output(outputSize, DString::ExplicitSize);
248 void *cd = portable_iconv_open(outputEncoding,inputEncoding);
249 if (cd==reinterpret_cast<void *>(-1))
250 {
251 return false;
252 }
253 bool ok=true;
254 size_t iLeft=inputSize;
255 size_t oLeft=outputSize;
256 const char *inputPtr = input.data();
257 char *outputPtr = output.rawData();
258 if (!portable_iconv(cd, &inputPtr, &iLeft, &outputPtr, &oLeft))
259 {
260 outputSize-=static_cast<int>(oLeft);
261 output.resize(outputSize);
262 output.at(outputSize)='\0';
263 // replace input
264 input=output.str();
265 //printf("iconv: input size=%d output size=%d\n[%s]\n",size,newSize,qPrint(srcBuf));
266 }
267 else
268 {
269 ok=false;
270 }
272 return ok;
273}
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:88
@ ExplicitSize
Definition dstring.h:135
int dstricmp(const char *s1, const char *s2)
Definition dstring.cpp:440
int portable_iconv_close(void *cd)
size_t portable_iconv(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
void * portable_iconv_open(const char *tocode, const char *fromcode)

References DString::at(), dstricmp(), DString::ExplicitSize, portable_iconv(), portable_iconv_close(), portable_iconv_open(), DString::rawData(), DString::resize(), and DString::str().

Referenced by LayoutDocManager::parse(), and readCodeFragment().

◆ writeUTF8Char()

const char * writeUTF8Char ( TextStream & t,
const char * s )

Writes the UTF8 character pointed to by s to stream t and returns a pointer to the next character.

Definition at line 198 of file utf8.cpp.

199{
200 if (s==nullptr) return nullptr;
201 uint8_t len = getUTF8CharNumBytes(*s);
202 for (uint8_t i=0;i<len;i++)
203 {
204 if (s[i]==0) // detect premature end of string (due to invalid UTF8 char)
205 {
206 len=i;
207 }
208 }
209 t.write(s,len);
210 return s+len;
211}
void write(const char *buf, size_t len)
Adds a array of character to the stream.
Definition textstream.h:204

References getUTF8CharNumBytes(), and TextStream::write().

Referenced by HtmlCodeGenerator::codify(), ManCodeGenerator::codify(), RTFCodeGenerator::codify(), HtmlDocVisitor::operator()(), HtmlDocVisitor::writeObfuscatedMailAddress(), and writeXMLCodeString().