LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
strwstrtools.h
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
16#ifndef LIMA_COMMONS_MISC_STRWSTRTOOLS_H
17#define LIMA_COMMONS_MISC_STRWSTRTOOLS_H
18
19#include "LimaString.h"
20
21#include <string>
22#include <vector>
23#include <iosfwd>
24#include <ostream>
25
26// // From ICU
27#define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
28#define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
29#define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
30#define U16_GET_SUPPLEMENTARY(lead, trail) \
31(((char32_t)(lead)<<10UL)+(char32_t)(trail)-U16_SURROGATE_OFFSET)
32
33#define U16_LEAD(supplementary) (char32_t)(((supplementary)>>10)+0xd7c0)
34#define U16_TRAIL(supplementary) (char32_t)(((supplementary)&0x3ff)|0xdc00)
35
36#define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800)
37#define U16_IS_SINGLE(c) !U_IS_SURROGATE(c)
38#define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
39#define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
40
41#ifdef WIN32
42#include <stdint.h>
43#endif
44
45#define U16_NEXT(s, i, length, c) { \
46(c)=(s)[(i)++].unicode(); \
47if(U16_IS_LEAD(c)) { \
48 uint16_t __c2; \
49 if((i)<(length) && U16_IS_TRAIL(__c2=(s)[(i)].unicode())) { \
50 ++(i); \
51 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
52 } \
53 } \
54 }
55
56#define U16_PREV(s, start, i, c) { \
57(c)=(s)[--(i)].unicode(); \
58if(U16_IS_TRAIL(c)) { \
59 uint16_t __c2; \
60 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1].unicode())) { \
61 --(i); \
62 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
63 } \
64} \
65}
66
67#define U_MASK(x) ((uint32_t)1<<(x))
68
69
70namespace Lima {
71namespace Common {
72namespace Misc {
73
74//
75// string <-> stream
76//
77
82LIMA_DATA_EXPORT void readStream (std::istream &is,std::string &dest);
83
88LIMA_DATA_EXPORT void writeStream(std::ostream &os,const std::string &src);
89
94//
95// reading and writing string and wstring in binary format
96//
101LIMA_DATA_EXPORT void readString(std::istream& file, std::string& s);
106LIMA_DATA_EXPORT void writeString(std::ostream& file, const std::string& s);
107
109// (use variable-byte coding for length of strings (less space used)
110LIMA_DATA_EXPORT void readStringField(std::istream& file, std::string& s);
111LIMA_DATA_EXPORT void writeStringField(std::ostream& file, const std::string& s);
113
120LIMA_DATA_EXPORT void readUTF8StringField(std::istream& file,
128LIMA_DATA_EXPORT void writeUTF8StringField(std::ostream& file,
129 const Lima::LimaString& s);
130
131
132//
133// string <-> limastring
134//
135
137
143
150LIMA_DATA_EXPORT std::string localestring2utf8string(const std::string &src);
151
156LIMA_DATA_EXPORT std::string limastring2utf8stdstring(const Lima::LimaString & phrase, uint32_t size );
157
163
164template < typename C >
165void chomp(std::basic_string< C >& str)
166{
167
168 if ( ( str.begin() != str.end() )
169 && ( *(--str.end()) == C('\n') ) ) str.erase(--str.end());
170}
171
172template < typename C >
173void chop(std::basic_string< C >& str)
174{
175
176 if ( str.begin() != str.end() ) str.erase(--str.end());
177}
178
179
180// read 1 line of text file in string
181LIMA_DATA_EXPORT std::string readLine(std::istream & inputFile);
182
183// convert any String to displayable string (for trace only)
184template <typename charT>
185std::string convertString( const std::basic_string<charT> &input ) {
186 static const char hexSymbol[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
187 std::string result;
188 for(typename std::basic_string<charT>::const_iterator i = input.begin() ; i != input.end() ; i++ ) {
189 result.append(" x");
190 uint64_t val = (*i).unicode();
191 if ( sizeof(charT) == 4 ) {
192 result.append( 1, hexSymbol[ (val>>28)&0xF ] );
193 result.append( 1, hexSymbol[ (val>>24)&0xF ] );
194 result.append( 1, hexSymbol[ (val>>20)&0xF ] );
195 result.append( 1, hexSymbol[ (val>>16)&0xF ] );
196 }
197 if ( sizeof(charT) >= 2 ) {
198 result.append( 1, hexSymbol[ (val>>12)&0xF ] );
199 result.append( 1, hexSymbol[ (val>>8)&0xF ] );
200 }
201 result.append( 1, hexSymbol[ (val>>4)&0xF ] );
202 result.append( 1, hexSymbol[ val&0x0F ] );
203 }
204 if ( sizeof(charT) == 1 ) {
205 const void *ptr = static_cast<const void*>(&input);
206 const std::string* strPtr = static_cast<const std::string*>(ptr);
207 const std::string text = *strPtr;
208 result += std::string("(") + text + std::string(")");
209 }
210 return result;
211}
212
213LIMA_DATA_EXPORT LimaString setStringSameEndian (const short unsigned int* s, const uint32_t count);
214LIMA_DATA_EXPORT LimaString setStringReverseEndian (const short unsigned int* s, const uint32_t count);
215
217
218
219
220} // closing namespace Misc
221} // closing namespace Common
222} // closing namespace Lima
223
224#endif // LIMA_COMMONS_MISC_STRWSTRTOOLS_H
#define LIMA_DATA_EXPORT
Definition LimaCommon.h:128
std::string localestring2utf8string(const std::string &src)
␈rief Convert a string in the locale codepage to a UTF8 string
void writeUTF8StringField(std::ostream &file, const Lima::LimaString &s)
Binary write a a LimaString in a file as an utf8 string.
void readString(istream &file, string &s)
Read an input stream into a wide string, until EOF.
void chomp(std::basic_string< C > &str)
std::string readLine(std::istream &inputFile)
void writeString(ostream &file, const string &s)
write a string on an output file stream
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
std::string convertString(const std::basic_string< charT > &input)
void readStringField(istream &file, string &s)
void readStream(std::istream &is, std::string &dest)
Read an input stream into a string, until EOF.
LimaString setStringSameEndian(const unsigned short *s, const uint32_t count)
redefine an existing LimaString from UTF-16
void writeStringField(ostream &file, const string &s)
void chop(std::basic_string< C > &str)
void readUTF8StringField(std::istream &file, Lima::LimaString &s)
Binary read an utf8 string from a file and store it in a LimaString.
LimaString transcodeToXmlEntities(const LimaString &str)
void writeStream(std::ostream &os, const std::string &src)
Write a string to an output stream.
LimaString latin15stdstring2limastring(const std::string &src)
Convert a latin15 string to a LimaString.
LimaString setStringReverseEndian(const unsigned short *s, const uint32_t count)
redefine an existing LimaString from UTF-16 changing the endian mode
LimaString utf8stdstring2limastring(const std::string &src)
NAUTITIA.
QString LimaString
Definition LimaString.h:33