shared code: use more modern c++: avoid c-style casts

This commit is contained in:
Jean-Francois Dockes 2021-09-23 11:20:24 +02:00
parent 7179e0dbf8
commit 4aeb9a8b78
3 changed files with 43 additions and 44 deletions

View File

@ -26,9 +26,10 @@
#include "md5.h"
#include <cstring>
#include <cstdint>
#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)
#define PUT_64BIT_LE(cp, value) do { \

View File

@ -294,25 +294,18 @@ template <class T> string stringsToString(const T& tokens)
return out;
}
template <class T> void stringsToCSV(const T& tokens, string& s,
char sep)
template <class T> void stringsToCSV(const T& tokens, string& s, char sep)
{
s.erase();
for (auto it = tokens.begin();
it != tokens.end(); it++) {
for (const auto& tok : tokens) {
bool needquotes = false;
if (it->empty() ||
it->find_first_of(string(1, sep) + "\"\n") != string::npos) {
if (tok.empty() || tok.find_first_of(string(1, sep) + "\"\n") != string::npos) {
needquotes = true;
}
if (it != tokens.begin()) {
s.append(1, sep);
}
if (needquotes) {
s.append(1, '"');
}
for (unsigned int i = 0; i < it->length(); i++) {
char car = it->at(i);
for (auto&& car : tok) {
if (car == '"') {
s.append(2, '"');
} else {
@ -322,7 +315,11 @@ template <class T> void stringsToCSV(const T& tokens, string& s,
if (needquotes) {
s.append(1, '"');
}
s.append(1, sep);
}
// Remove last separator.
if (s.size())
s.pop_back();
}
#ifdef SMALLUT_EXTERNAL_INSTANTIATIONS

View File

@ -21,6 +21,7 @@
#include "assert.h"
#endif
#include <string>
#include <cstdint>
/**
* 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
* position at best. This can only be useful for a lookahead from the
* 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;
unsigned int mycp = 0;
if (charpos >= m_charpos) {
@ -71,7 +72,7 @@ public:
while (mypos < m_sp->length() && mycp != charpos) {
l = get_cl(mypos);
if (l <= 0 || !poslok(mypos, l) || !checkvalidat(mypos, l))
return (unsigned int)-1;
return uint32_t(-1);
mypos += l;
++mycp;
}
@ -80,7 +81,7 @@ public:
if (poslok(mypos, l) && checkvalidat(mypos, l))
return getvalueat(mypos, l);
}
return (unsigned int)-1;
return uint32_t(-1);
}
/** Increment current position to next utf-8 char */
@ -100,11 +101,11 @@ public:
}
/** operator* returns the ucs4 value as a machine integer*/
unsigned int operator*() {
uint32_t operator*() {
#ifdef UTF8ITER_CHECK
assert(m_cl > 0);
#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.
@ -186,20 +187,20 @@ private:
inline bool checkvalidat(std::string::size_type p, int l) const {
switch (l) {
case 1:
return (unsigned char)(*m_sp)[p] < 128;
return uint8_t((*m_sp)[p]) < 128;
case 2:
return (((unsigned char)(*m_sp)[p]) & 224) == 192
&& (((unsigned char)(*m_sp)[p+1]) & 192) == 128;
return uint8_t((*m_sp)[p] & 224) == 192
&& uint8_t((*m_sp)[p+1] & 192) == 128;
case 3:
return (((unsigned char)(*m_sp)[p]) & 240) == 224
&& (((unsigned char)(*m_sp)[p+1]) & 192) == 128
&& (((unsigned char)(*m_sp)[p+2]) & 192) == 128
return uint8_t((*m_sp)[p] & 240) == 224
&& uint8_t((*m_sp)[p+1] & 192) == 128
&& uint8_t((*m_sp)[p+2] & 192) == 128
;
case 4:
return (((unsigned char)(*m_sp)[p]) & 248) == 240
&& (((unsigned char)(*m_sp)[p+1]) & 192) == 128
&& (((unsigned char)(*m_sp)[p+2]) & 192) == 128
&& (((unsigned char)(*m_sp)[p+3]) & 192) == 128
return uint8_t((*m_sp)[p] & 248) == 240
&& uint8_t((*m_sp)[p+1] & 192) == 128
&& uint8_t((*m_sp)[p+2] & 192) == 128
&& uint8_t((*m_sp)[p+3] & 192) == 128
;
default:
return false;
@ -208,7 +209,7 @@ private:
// Get character byte length at specified position. Returns 0 for error.
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) {
return 1;
} else if ((z & 224) == 192) {
@ -232,16 +233,16 @@ private:
#ifdef UTF8ITER_CHECK
assert((unsigned char)(*m_sp)[p] < 128);
#endif
return (unsigned char)(*m_sp)[p];
return uint8_t((*m_sp)[p]);
case 2:
#ifdef UTF8ITER_CHECK
assert(
((unsigned char)(*m_sp)[p] & 224) == 192
uint8_t((*m_sp)[p] & 224) == 192
&& ((unsigned char)(*m_sp)[p+1] & 192) == 128
);
#endif
return ((unsigned char)(*m_sp)[p] - 192) * 64 +
(unsigned char)(*m_sp)[p+1] - 128 ;
return uint8_t((*m_sp)[p] - 192) * 64 +
uint8_t((*m_sp)[p+1] - 128);
case 3:
#ifdef UTF8ITER_CHECK
assert(
@ -251,29 +252,29 @@ private:
);
#endif
return ((unsigned char)(*m_sp)[p] - 224) * 4096 +
((unsigned char)(*m_sp)[p+1] - 128) * 64 +
(unsigned char)(*m_sp)[p+2] - 128;
return uint8_t((*m_sp)[p] - 224) * 4096 +
uint8_t((*m_sp)[p+1] - 128) * 64 +
uint8_t((*m_sp)[p+2] - 128);
case 4:
#ifdef UTF8ITER_CHECK
assert(
(((unsigned char)(*m_sp)[p]) & 248) == 240
&& (((unsigned char)(*m_sp)[p+1]) & 192) == 128
&& (((unsigned char)(*m_sp)[p+2]) & 192) == 128
&& (((unsigned char)(*m_sp)[p+3]) & 192) == 128
uint8_t((*m_sp)[p] & 248) == 240
&& uint8_t((*m_sp)[p+1] & 192) == 128
&& uint8_t((*m_sp)[p+2] & 192) == 128
&& uint8_t((*m_sp)[p+3] & 192) == 128
);
#endif
return ((unsigned char)(*m_sp)[p]-240)*262144 +
((unsigned char)(*m_sp)[p+1]-128)*4096 +
((unsigned char)(*m_sp)[p+2]-128)*64 +
(unsigned char)(*m_sp)[p+3]-128;
return uint8_t((*m_sp)[p]-240)*262144 +
uint8_t((*m_sp)[p+1]-128)*4096 +
uint8_t((*m_sp)[p+2]-128)*64 +
uint8_t((*m_sp)[p+3]-128);
default:
#ifdef UTF8ITER_CHECK
assert(l <= 4);
#endif
return (unsigned int)-1;
return uint32_t(-1);
}
}