shared code: use more modern c++: avoid c-style casts
This commit is contained in:
parent
7179e0dbf8
commit
4aeb9a8b78
@ -26,9 +26,10 @@
|
|||||||
#include "md5.h"
|
#include "md5.h"
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
#define PUT_BIT_LE(i, cp, value) do { \
|
#define PUT_BIT_LE(i, cp, value) do { \
|
||||||
(cp)[i] = (uint8_t)(((value) >> 8 * i) & 0xFF); \
|
(cp)[i] = uint8_t(((value) >> 8 * i) & 0xFF); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define PUT_64BIT_LE(cp, value) do { \
|
#define PUT_64BIT_LE(cp, value) do { \
|
||||||
|
|||||||
@ -294,25 +294,18 @@ template <class T> string stringsToString(const T& tokens)
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T> void stringsToCSV(const T& tokens, string& s,
|
template <class T> void stringsToCSV(const T& tokens, string& s, char sep)
|
||||||
char sep)
|
|
||||||
{
|
{
|
||||||
s.erase();
|
s.erase();
|
||||||
for (auto it = tokens.begin();
|
for (const auto& tok : tokens) {
|
||||||
it != tokens.end(); it++) {
|
|
||||||
bool needquotes = false;
|
bool needquotes = false;
|
||||||
if (it->empty() ||
|
if (tok.empty() || tok.find_first_of(string(1, sep) + "\"\n") != string::npos) {
|
||||||
it->find_first_of(string(1, sep) + "\"\n") != string::npos) {
|
|
||||||
needquotes = true;
|
needquotes = true;
|
||||||
}
|
}
|
||||||
if (it != tokens.begin()) {
|
|
||||||
s.append(1, sep);
|
|
||||||
}
|
|
||||||
if (needquotes) {
|
if (needquotes) {
|
||||||
s.append(1, '"');
|
s.append(1, '"');
|
||||||
}
|
}
|
||||||
for (unsigned int i = 0; i < it->length(); i++) {
|
for (auto&& car : tok) {
|
||||||
char car = it->at(i);
|
|
||||||
if (car == '"') {
|
if (car == '"') {
|
||||||
s.append(2, '"');
|
s.append(2, '"');
|
||||||
} else {
|
} else {
|
||||||
@ -322,7 +315,11 @@ template <class T> void stringsToCSV(const T& tokens, string& s,
|
|||||||
if (needquotes) {
|
if (needquotes) {
|
||||||
s.append(1, '"');
|
s.append(1, '"');
|
||||||
}
|
}
|
||||||
|
s.append(1, sep);
|
||||||
}
|
}
|
||||||
|
// Remove last separator.
|
||||||
|
if (s.size())
|
||||||
|
s.pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SMALLUT_EXTERNAL_INSTANTIATIONS
|
#ifdef SMALLUT_EXTERNAL_INSTANTIATIONS
|
||||||
|
|||||||
@ -21,6 +21,7 @@
|
|||||||
#include "assert.h"
|
#include "assert.h"
|
||||||
#endif
|
#endif
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A small helper class to iterate over utf8 strings. This is not an
|
* A small helper class to iterate over utf8 strings. This is not an
|
||||||
@ -60,7 +61,7 @@ public:
|
|||||||
/** "Direct" access. Awfully inefficient as we skip from start or current
|
/** "Direct" access. Awfully inefficient as we skip from start or current
|
||||||
* position at best. This can only be useful for a lookahead from the
|
* position at best. This can only be useful for a lookahead from the
|
||||||
* current position */
|
* current position */
|
||||||
unsigned int operator[](std::string::size_type charpos) const {
|
uint32_t operator[](std::string::size_type charpos) const {
|
||||||
std::string::size_type mypos = 0;
|
std::string::size_type mypos = 0;
|
||||||
unsigned int mycp = 0;
|
unsigned int mycp = 0;
|
||||||
if (charpos >= m_charpos) {
|
if (charpos >= m_charpos) {
|
||||||
@ -71,7 +72,7 @@ public:
|
|||||||
while (mypos < m_sp->length() && mycp != charpos) {
|
while (mypos < m_sp->length() && mycp != charpos) {
|
||||||
l = get_cl(mypos);
|
l = get_cl(mypos);
|
||||||
if (l <= 0 || !poslok(mypos, l) || !checkvalidat(mypos, l))
|
if (l <= 0 || !poslok(mypos, l) || !checkvalidat(mypos, l))
|
||||||
return (unsigned int)-1;
|
return uint32_t(-1);
|
||||||
mypos += l;
|
mypos += l;
|
||||||
++mycp;
|
++mycp;
|
||||||
}
|
}
|
||||||
@ -80,7 +81,7 @@ public:
|
|||||||
if (poslok(mypos, l) && checkvalidat(mypos, l))
|
if (poslok(mypos, l) && checkvalidat(mypos, l))
|
||||||
return getvalueat(mypos, l);
|
return getvalueat(mypos, l);
|
||||||
}
|
}
|
||||||
return (unsigned int)-1;
|
return uint32_t(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Increment current position to next utf-8 char */
|
/** Increment current position to next utf-8 char */
|
||||||
@ -100,11 +101,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** operator* returns the ucs4 value as a machine integer*/
|
/** operator* returns the ucs4 value as a machine integer*/
|
||||||
unsigned int operator*() {
|
uint32_t operator*() {
|
||||||
#ifdef UTF8ITER_CHECK
|
#ifdef UTF8ITER_CHECK
|
||||||
assert(m_cl > 0);
|
assert(m_cl > 0);
|
||||||
#endif
|
#endif
|
||||||
return m_cl == 0 ? (unsigned int)-1 : getvalueat(m_pos, m_cl);
|
return m_cl == 0 ? uint32_t(-1) : getvalueat(m_pos, m_cl);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Append current utf-8 possibly multi-byte character to string param.
|
/** Append current utf-8 possibly multi-byte character to string param.
|
||||||
@ -186,20 +187,20 @@ private:
|
|||||||
inline bool checkvalidat(std::string::size_type p, int l) const {
|
inline bool checkvalidat(std::string::size_type p, int l) const {
|
||||||
switch (l) {
|
switch (l) {
|
||||||
case 1:
|
case 1:
|
||||||
return (unsigned char)(*m_sp)[p] < 128;
|
return uint8_t((*m_sp)[p]) < 128;
|
||||||
case 2:
|
case 2:
|
||||||
return (((unsigned char)(*m_sp)[p]) & 224) == 192
|
return uint8_t((*m_sp)[p] & 224) == 192
|
||||||
&& (((unsigned char)(*m_sp)[p+1]) & 192) == 128;
|
&& uint8_t((*m_sp)[p+1] & 192) == 128;
|
||||||
case 3:
|
case 3:
|
||||||
return (((unsigned char)(*m_sp)[p]) & 240) == 224
|
return uint8_t((*m_sp)[p] & 240) == 224
|
||||||
&& (((unsigned char)(*m_sp)[p+1]) & 192) == 128
|
&& uint8_t((*m_sp)[p+1] & 192) == 128
|
||||||
&& (((unsigned char)(*m_sp)[p+2]) & 192) == 128
|
&& uint8_t((*m_sp)[p+2] & 192) == 128
|
||||||
;
|
;
|
||||||
case 4:
|
case 4:
|
||||||
return (((unsigned char)(*m_sp)[p]) & 248) == 240
|
return uint8_t((*m_sp)[p] & 248) == 240
|
||||||
&& (((unsigned char)(*m_sp)[p+1]) & 192) == 128
|
&& uint8_t((*m_sp)[p+1] & 192) == 128
|
||||||
&& (((unsigned char)(*m_sp)[p+2]) & 192) == 128
|
&& uint8_t((*m_sp)[p+2] & 192) == 128
|
||||||
&& (((unsigned char)(*m_sp)[p+3]) & 192) == 128
|
&& uint8_t((*m_sp)[p+3] & 192) == 128
|
||||||
;
|
;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
@ -208,7 +209,7 @@ private:
|
|||||||
|
|
||||||
// Get character byte length at specified position. Returns 0 for error.
|
// Get character byte length at specified position. Returns 0 for error.
|
||||||
inline int get_cl(std::string::size_type p) const {
|
inline int get_cl(std::string::size_type p) const {
|
||||||
unsigned int z = (unsigned char)(*m_sp)[p];
|
unsigned int z = uint8_t((*m_sp)[p]);
|
||||||
if (z <= 127) {
|
if (z <= 127) {
|
||||||
return 1;
|
return 1;
|
||||||
} else if ((z & 224) == 192) {
|
} else if ((z & 224) == 192) {
|
||||||
@ -232,16 +233,16 @@ private:
|
|||||||
#ifdef UTF8ITER_CHECK
|
#ifdef UTF8ITER_CHECK
|
||||||
assert((unsigned char)(*m_sp)[p] < 128);
|
assert((unsigned char)(*m_sp)[p] < 128);
|
||||||
#endif
|
#endif
|
||||||
return (unsigned char)(*m_sp)[p];
|
return uint8_t((*m_sp)[p]);
|
||||||
case 2:
|
case 2:
|
||||||
#ifdef UTF8ITER_CHECK
|
#ifdef UTF8ITER_CHECK
|
||||||
assert(
|
assert(
|
||||||
((unsigned char)(*m_sp)[p] & 224) == 192
|
uint8_t((*m_sp)[p] & 224) == 192
|
||||||
&& ((unsigned char)(*m_sp)[p+1] & 192) == 128
|
&& ((unsigned char)(*m_sp)[p+1] & 192) == 128
|
||||||
);
|
);
|
||||||
#endif
|
#endif
|
||||||
return ((unsigned char)(*m_sp)[p] - 192) * 64 +
|
return uint8_t((*m_sp)[p] - 192) * 64 +
|
||||||
(unsigned char)(*m_sp)[p+1] - 128 ;
|
uint8_t((*m_sp)[p+1] - 128);
|
||||||
case 3:
|
case 3:
|
||||||
#ifdef UTF8ITER_CHECK
|
#ifdef UTF8ITER_CHECK
|
||||||
assert(
|
assert(
|
||||||
@ -251,29 +252,29 @@ private:
|
|||||||
);
|
);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return ((unsigned char)(*m_sp)[p] - 224) * 4096 +
|
return uint8_t((*m_sp)[p] - 224) * 4096 +
|
||||||
((unsigned char)(*m_sp)[p+1] - 128) * 64 +
|
uint8_t((*m_sp)[p+1] - 128) * 64 +
|
||||||
(unsigned char)(*m_sp)[p+2] - 128;
|
uint8_t((*m_sp)[p+2] - 128);
|
||||||
case 4:
|
case 4:
|
||||||
#ifdef UTF8ITER_CHECK
|
#ifdef UTF8ITER_CHECK
|
||||||
assert(
|
assert(
|
||||||
(((unsigned char)(*m_sp)[p]) & 248) == 240
|
uint8_t((*m_sp)[p] & 248) == 240
|
||||||
&& (((unsigned char)(*m_sp)[p+1]) & 192) == 128
|
&& uint8_t((*m_sp)[p+1] & 192) == 128
|
||||||
&& (((unsigned char)(*m_sp)[p+2]) & 192) == 128
|
&& uint8_t((*m_sp)[p+2] & 192) == 128
|
||||||
&& (((unsigned char)(*m_sp)[p+3]) & 192) == 128
|
&& uint8_t((*m_sp)[p+3] & 192) == 128
|
||||||
);
|
);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return ((unsigned char)(*m_sp)[p]-240)*262144 +
|
return uint8_t((*m_sp)[p]-240)*262144 +
|
||||||
((unsigned char)(*m_sp)[p+1]-128)*4096 +
|
uint8_t((*m_sp)[p+1]-128)*4096 +
|
||||||
((unsigned char)(*m_sp)[p+2]-128)*64 +
|
uint8_t((*m_sp)[p+2]-128)*64 +
|
||||||
(unsigned char)(*m_sp)[p+3]-128;
|
uint8_t((*m_sp)[p+3]-128);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
#ifdef UTF8ITER_CHECK
|
#ifdef UTF8ITER_CHECK
|
||||||
assert(l <= 4);
|
assert(l <= 4);
|
||||||
#endif
|
#endif
|
||||||
return (unsigned int)-1;
|
return uint32_t(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user