diff --git a/etk/Buffer.h b/etk/Buffer.h index 151cbcb..1c82ae6 100644 --- a/etk/Buffer.h +++ b/etk/Buffer.h @@ -40,13 +40,11 @@ |____________________________________________________________________________________| */ -namespace etk -{ +namespace etk { /** * @brief Buffer classes. Designed for access o */ - class Buffer - { + class Buffer { private: int8_t* m_data; //!< pointer on the curetn table of Data int32_t m_allocated; //!< Current allocated size @@ -216,13 +214,21 @@ namespace etk tmpBuffer.clear(); if (_pos < m_gapStart) { if (_pos + _nbElement < m_gapStart) { - tmpBuffer.push_back(&m_data[_pos], _nbElement); + for (size_t iii = _pos; iii<_pos+_nbElement; ++iii) { + tmpBuffer.push_back(m_data[iii]); + } } else { - tmpBuffer.push_back(&m_data[_pos], m_gapStart - _pos); - tmpBuffer.push_back(&m_data[m_gapEnd], _nbElement - (m_gapStart - _pos) ); + for (size_t iii = _pos; iii& _items) { - insert(_pos, _items.dataPointer(), _items.size()); + insert(_pos, &_items[0], _items.size()); } /** * @brief Insert data in the buffer @@ -324,7 +330,7 @@ namespace etk * @param[in] _items Data that might be inserted. */ void replace(int32_t _pos, int32_t _nbRemoveElement, std::vector& _items) { - replace(_pos, _nbRemoveElement, _items.dataPointer(), _items.size()); + replace(_pos, _nbRemoveElement, &_items[0], _items.size()); } /** * @brief Replace specified data. @@ -386,7 +392,7 @@ namespace etk /** * @brief Remove the last element of the Buffer. */ - void popBack(void) { + void pop_back(void) { if (size()>0) { remove( size() ); } diff --git a/etk/Color.cpp b/etk/Color.cpp index 48fe474..88619af 100644 --- a/etk/Color.cpp +++ b/etk/Color.cpp @@ -12,6 +12,7 @@ #include #include #include +#include static bool strnCmpNoCase(const char * input1, const char * input2, int32_t maxLen) { int32_t iii=0; @@ -89,67 +90,68 @@ namespace etk { m_g(255), m_b(255), m_a(255) { - if ( _input.size() >=1 - && _input[0] == '#') { - // remove '#' - _input.erase(0, 1); - uint32_t val = 0; - //std::ostringstream oss; - //oss << to_u8string(_input); - //oss >> std::hex >> val; - val = std::stoul(_input); - if(_input.size() == 3) { - m_r = (val & 0x00000F00) >> 8; - m_r = (m_r | m_r << 4); - m_g = (val & 0x000000F0) >> 4; - m_g = (m_g | m_g << 4); - m_b = (val & 0x0000000F) >> 0; - m_b = (m_b | m_b << 4); - } else if (_input.size() == 4) { - m_r = (val & 0x0000F000) >> 12; - m_r = (m_r | m_r << 4); - m_g = (val & 0x00000F00) >> 8; - m_g = (m_g | m_g << 4); - m_b = (val & 0x000000F0) >> 4; - m_b = (m_b | m_b << 4); - m_a = (val & 0x0000000F) >> 0; - m_a = (m_a | m_a << 4); - } else if (_input.size() == 6) { - m_r = (val & 0x00FF0000) >> 16; - m_r = (m_r | m_r << 4); - m_g = (val & 0x0000FF00) >> 8; - m_g = (m_g | m_g << 4); - m_b = (val & 0x000000FF) >> 0; - m_b = (m_b | m_b << 4); - } else if (_input.size() == 8) { - m_r = (val & 0xFF000000) >> 24; - m_r = (m_r | m_r << 4); - m_g = (val & 0x00FF0000) >> 16; - m_g = (m_g | m_g << 4); - m_b = (val & 0x0000FF00) >> 8; - m_b = (m_b | m_b << 4); - m_a = (val & 0x000000FF) >> 0; - m_a = (m_a | m_a << 4); + const char* inputData = _input.c_str(); + size_t len = _input.size(); + if( len >=1 + && inputData[0] == '#') { + if(len == 4) { + int32_t red=0, green=0, blue=0; + if (sscanf(inputData + 1, "%1x%1x%1x", &red, &green, &blue) == 3) { + m_r = (red | red << 4); + m_g = (green | green << 4); + m_b = (blue | blue << 4); + } else { + TK_ERROR(" pb in parsing the color : \"" << inputData << "\""); + } + } else if (len==5) { + int32_t red=0, green=0, blue=0, alpha=0; + if (sscanf(inputData + 1, "%1x%1x%1x%1x", &red, &green, &blue, &alpha) == 4) { + m_r = (red | red << 4); + m_g = (green | green << 4); + m_b = (blue | blue << 4); + m_a = (alpha | alpha << 4); + } else { + TK_ERROR(" pb in parsing the color : \"" << inputData << "\""); + } + } else if (len == 7) { + int32_t red=0, green=0, blue=0; + if (sscanf(inputData + 1, "%2x%2x%2x", &red, &green, &blue) == 3) { + m_r = red; + m_g = green; + m_b = blue; + } else { + TK_ERROR(" pb in parsing the color : \"" << inputData << "\""); + } + } else if (len == 9) { + int32_t red=0, green=0, blue=0, alpha=0; + if (sscanf(inputData + 1, "%2x%2x%2x%2x", &red, &green, &blue, &alpha) == 4) { + m_r = red; + m_g = green; + m_b = blue; + m_a = alpha; + } else { + TK_ERROR(" pb in parsing the color : \"" << inputData << "\""); + } } else { - TK_ERROR(" pb in parsing the color : \"" << _input << "\" ==> unknown methode ..."); + TK_ERROR(" pb in parsing the color : \"" << inputData << "\" ==> unknown methode ..."); } - } else if( _input.size() >= 4 - && _input[0] == 'r' - && _input[1] == 'g' - && _input[2] == 'b' - && _input[3] == '(' ) { - int32_t _red=0, _green=0, _blue=0, _alpha=0; + } else if( len >= 4 + && inputData[0] == 'r' + && inputData[1] == 'g' + && inputData[2] == 'b' + && inputData[3] == '(' ) { + int32_t red=0, green=0, blue=0, alpha=0; float fred=0, fgreen=0, fblue=0, falpha=0; - if (sscanf(_input.c_str() + 4, "%u,%u,%u,%u", &_red, &_green, &_blue, &_alpha) == 4) { - m_r = etk_min(0xFF, _red); - m_g = etk_min(0xFF, _green); - m_b = etk_min(0xFF, _blue); - m_a = etk_min(0xFF, _alpha); - } else if (sscanf(_input.c_str() + 4, "%u,%u,%u", &_red, &_green, &_blue) == 3) { - m_r = etk_min(0xFF, _red); - m_g = etk_min(0xFF, _green); - m_b = etk_min(0xFF, _blue); - } else if (sscanf(_input.c_str() + 4, "%f%%,%f%%,%f%%,%f%%", &fred, &fgreen, &fblue, &falpha) == 4) { + if (sscanf(inputData + 4, "%u,%u,%u,%u", &red, &green, &blue, &alpha) == 4) { + m_r = etk_min(0xFF, red); + m_g = etk_min(0xFF, green); + m_b = etk_min(0xFF, blue); + m_a = etk_min(0xFF, alpha); + } else if (sscanf(inputData + 4, "%u,%u,%u", &red, &green, &blue) == 3) { + m_r = etk_min(0xFF, red); + m_g = etk_min(0xFF, green); + m_b = etk_min(0xFF, blue); + } else if (sscanf(inputData + 4, "%f%%,%f%%,%f%%,%f%%", &fred, &fgreen, &fblue, &falpha) == 4) { fred = etk_avg(0.0, fred, 1.0); fgreen = etk_avg(0.0, fgreen, 1.0); fblue = etk_avg(0.0, fblue, 1.0); @@ -158,7 +160,7 @@ namespace etk { m_g = (uint8_t)(fgreen * 255.); m_b = (uint8_t)(fblue * 255.); m_a = (uint8_t)(falpha * 255.); - } else if (sscanf(_input.c_str() + 4, "%f%%,%f%%,%f%%", &fred, &fgreen, &fblue) == 3) { + } else if (sscanf(inputData + 4, "%f%%,%f%%,%f%%", &fred, &fgreen, &fblue) == 3) { fred = etk_avg(0.0, fred, 1.0); fgreen= etk_avg(0.0, fgreen, 1.0); fblue = etk_avg(0.0, fblue, 1.0); @@ -166,13 +168,13 @@ namespace etk { m_g = (uint8_t)(fgreen * 255.); m_b = (uint8_t)(fblue * 255.); } else { - TK_ERROR(" pb in parsing the color : \"" << _input << "\" ==> unknown methode ..."); + TK_ERROR(" pb in parsing the color : \"" << inputData << "\" ==> unknown methode ..."); } } else { bool findIt = false; // direct named color ... for (esize_t iii=0; iii " << *this); + TK_VERBOSE("Parse color : \"" << inputData << "\" ==> " << *this); } template<> Color::Color(std::string _input) { diff --git a/etk/RegExp.h b/etk/RegExp.h index 7990bfa..4041213 100644 --- a/etk/RegExp.h +++ b/etk/RegExp.h @@ -164,8 +164,7 @@ template class RegExpNodeValue : public RegExpNode */ ~RegExpNodeValue(void) { }; - int32_t generate(const std::vector& _data) - { + int32_t generate(const std::vector& _data) { RegExpNode::m_RegExpData = _data; TK_REG_EXP_DBG_MODE("Request Parse \"Value\" data="; displayElem(RegExpNode::m_RegExpData);); m_data.clear(); @@ -1199,6 +1198,12 @@ template class RegExpNodePThese : public RegExpNode::m_RegExpData);); + } }; #undef __class__ #define __class__ "etk::RegExp" @@ -1231,7 +1236,7 @@ template class RegExp setRegExp(_exp); } }; - RegExp(const std::string &_exp="") : + RegExp(const std::string &_exp) : m_expressionRequested(U""), m_isOk(false), m_notBeginWithChar(false), @@ -1256,6 +1261,7 @@ template class RegExp */ void setRegExp(const std::string &_exp) { if (_exp.size() != 0) { + TK_REG_EXP_DBG_MODE("normal string parse : '" << _exp << "'"); setRegExp(to_u32string(_exp)); } } @@ -1278,7 +1284,7 @@ template class RegExp int32_t countPTheseOut = 0; int32_t countBracketIn = 0; int32_t countBracketOut = 0; - for (esize_t iii=0; iii<_regexp.size(); iii++) { + for (size_t iii=0; iii<_regexp.size(); iii++) { if (_regexp[iii] == '\\') { if(iii+1>=_regexp.size()) { TK_ERROR("Dangerous parse of the element pos " << iii << " \\ with nothing after"); @@ -1378,14 +1384,15 @@ template class RegExp //TK_DEBUG("=> must not end with char"); m_notEndWithChar = true; // remove element - tmpExp.erase(tmpExp.end()); + tmpExp.erase(tmpExp.end()-1); } if (tmpExp.size() != m_exprRootNode.generate(tmpExp) ) { return; } // TODO : optimize node here ... - //Display(); + //drawColoredRegEx(); + //display(); // all OK ... play again m_isOk = true; @@ -1423,8 +1430,7 @@ template class RegExp bool process(const CLASS_TYPE& _SearchIn, esize_t _startPos, esize_t _endPos, - char32_t _escapeChar=0) - { + char32_t _escapeChar=0) { if (false == m_isOk) { return false; } @@ -1498,11 +1504,10 @@ template class RegExp return false; }; - bool processOneElement( const CLASS_TYPE& _SearchIn, - esize_t _startPos, - esize_t _endPos, - char32_t _escapeChar=0) - { + bool processOneElement(const CLASS_TYPE& _SearchIn, + esize_t _startPos, + esize_t _endPos, + char32_t _escapeChar=0) { if (false == m_isOk) { return false; } @@ -1567,29 +1572,37 @@ template class RegExp * @brief Get the expression start position detected * @return position of the start regExp */ - int32_t start(void) { return m_areaFind.start; }; + int32_t start(void) { + return m_areaFind.start; + }; /** * @brief Get the expression stop position detected * @return position of the stop regExp */ - int32_t stop(void) { return m_areaFind.stop; }; + int32_t stop(void) { + return m_areaFind.stop; + }; /** * @brief Display the reg Exp */ - void display(void) - { + void display(void) { m_exprRootNode.display(0); }; + /** + * @brief Just display the regExp in color ... + */ + void drawColoredRegEx(void) { + m_exprRootNode.drawColoredRegEx(); + } private: /** * @brief * @param[in,out] * @return */ - bool checkGoodPosition(const std::vector& _tmpExp, esize_t& _pos) - { + bool checkGoodPosition(const std::vector& _tmpExp, esize_t& _pos) { char32_t curentCode = _tmpExp[_pos]; char32_t endCode = REGEXP_OPCODE_PTHESE_OUT; const char *input = "(...)"; @@ -1693,8 +1706,7 @@ template class RegExp * @param[in,out] * @return */ - bool checkGoodPosition(const std::vector& _tmpExp) - { + bool checkGoodPosition(const std::vector& _tmpExp) { esize_t pos = 0; while (pos < _tmpExp.size()) { //TK_DEBUG("check : " << tmpExp[pos]); diff --git a/etk/UChar.cpp b/etk/UChar.cpp index 74087c7..688a85b 100644 --- a/etk/UChar.cpp +++ b/etk/UChar.cpp @@ -217,6 +217,35 @@ char32_t etk::setUtf8(const char* _input) { } } +int8_t etk::UChar::theoricUTF8Len(const char _input) { + if((_input&0x80) == 0x00 ) { + return 1; + } + if((_input&0xE0) == 0xC0) { + return 2; + } + if((_input&0xF0) == 0xE0) { + return 3; + } + if((_input&0xF8) == 0xF0) { + return 4; + } + return 1; +} + +bool etk::UChar::theoricUTF8First(const char _input) { + // When started with the bit 0 then the size is signle element. + if((_input&0x80) == 0x00 ) { + return true; + } + // for multiple element size, we just need to check the second element (might be != 1) + if((_input&0x40) == 0x40 ) { + return true; + } + return false; +} + + #if 0 @@ -357,34 +386,4 @@ int8_t char32_t::setUtf8(const char* _input) } } -int8_t char32_t::theoricUTF8Len(const char _input) -{ - if((_input&0x80) == 0x00 ) { - return 1; - } - if((_input&0xE0) == 0xC0) { - return 2; - } - if((_input&0xF0) == 0xE0) { - return 3; - } - if((_input&0xF8) == 0xF0) { - return 4; - } - return 1; -} - -bool char32_t::theoricUTF8First(const char _input) -{ - // When started with the bit 0 then the size is signle element. - if((_input&0x80) == 0x00 ) { - return true; - } - // for multiple element size, we just need to check the second element (might be != 1) - if((_input&0x40) == 0x40 ) { - return true; - } - return false; -} - #endif diff --git a/etk/UChar.h b/etk/UChar.h index 8a6c651..75dc499 100644 --- a/etk/UChar.h +++ b/etk/UChar.h @@ -47,6 +47,18 @@ namespace etk { extern const char32_t Delete; //!< DEL extern const char32_t Space; //!< ' ' SPACE extern const char32_t Escape; //!< ESC Escape + /** + * @brief Get the size of an utf8 char with his first char. + * @param[in] _input Char to parse + * @return number of char needed + */ + int8_t theoricUTF8Len(const char _input); + /** + * @brief When parsing a string in a reverse mode, we need to know if we get the first char + * @param[in] _input Char to parse. + * @return true if it was the first char. + */ + bool theoricUTF8First(const char _input); }; #if 0 class UChar : public char32_t{ @@ -69,18 +81,6 @@ namespace etk { //std::vector GetUtf8(void) const; int8_t setUtf8(const char* _input); public: - /** - * @brief Get the size of an utf8 char with his first char. - * @param[in] _input Char to parse - * @return number of char needed - */ - static int8_t theoricUTF8Len(const char _input); - /** - * @brief When parsing a string in a reverse mode, we need to know if we get the first char - * @param[in] _input Char to parse. - * @return true if it was the first char. - */ - static bool theoricUTF8First(const char _input); }; #endif /** diff --git a/etk/UString.cpp b/etk/UString.cpp index cfe038c..f43d3d4 100644 --- a/etk/UString.cpp +++ b/etk/UString.cpp @@ -47,55 +47,67 @@ etk::CCout& etk::operator <<(etk::CCout& _os, const std::vector& return _os; } -std::string to_u8string(const std::u32string& _obj) { - std::vector tmpp; - for (size_t iii=0; iii<_obj.size(); ++iii) { - tmpp.push_back(_obj[iii]); +std::string to_u8string(const std::u32string& _input) { + std::string out; + for (int32_t iii=0; iii<_input.size(); ++iii) { + char output[10]; + etk::getUtf8(_input[iii], output); + out += output; } - std::vector output_UTF8; - unicode::convertUnicodeToUtf8(tmpp, output_UTF8); - output_UTF8.push_back('\0'); - std::string out = &output_UTF8[0]; return out; } -std::u32string to_u32string(const std::string& _obj) { - // TODO : Change this ... - std::vector transformData; - for ( size_t iii=0; iii< _obj.size(); ++iii) { - transformData.push_back(_obj[iii]); - } - std::vector output_Unicode; - unicode::convertUtf8ToUnicode(transformData, output_Unicode); - if( 0 == output_Unicode.size() - || output_Unicode[output_Unicode.size()-1] != 0) { - return U""; - } - std::u32string out; - for ( size_t iii=0; iii< output_Unicode.size(); ++iii) { - transformData.push_back((char32_t)output_Unicode[iii]); - } - return out; +std::u32string to_u32string(const std::string& _input) { + return to_u32string(_input.c_str()); } -std::u32string to_u32string(const char* _obj) { - if (_obj == NULL) { - return U""; - } - int64_t len = strlen(_obj); - // TODO : Change this ... - std::vector transformData; - for ( size_t iii=0; iii < len; ++iii) { - transformData.push_back(_obj[iii]); - } - std::vector output_Unicode; - unicode::convertUtf8ToUnicode(transformData, output_Unicode); - if( 0 == output_Unicode.size() - || output_Unicode[output_Unicode.size()-1] != 0) { +std::u32string to_u32string(const char* _input) { + if (_input == NULL) { return U""; } std::u32string out; - for ( size_t iii=0; iii< output_Unicode.size(); ++iii) { - transformData.push_back((char32_t)output_Unicode[iii]); + char tmpData[20]; + int64_t pos = 0; + int64_t inputLen = strlen(_input); + while (pos < inputLen) { + int32_t lenMax = inputLen - pos; + //4 case + if ( 1<=lenMax + && 0x00 == (_input[pos+0] & 0x80) ) { + tmpData[0] = _input[pos+0]; + tmpData[1] = '\0'; + pos += 1; + } else if ( 2<=lenMax + && 0xC0 == (_input[pos+0] & 0xE0) + && 0x80 == (_input[pos+1] & 0xC0) ) { + tmpData[0] = _input[pos+0]; + tmpData[1] = _input[pos+1]; + tmpData[2] = '\0'; + pos += 2; + } else if ( 3<=lenMax + && 0xE0 == (_input[pos+0] & 0xF0) + && 0x80 == (_input[pos+1] & 0xC0) + && 0x80 == (_input[pos+2] & 0xC0)) { + tmpData[0] = _input[pos+0]; + tmpData[1] = _input[pos+1]; + tmpData[2] = _input[pos+2]; + tmpData[3] = '\0'; + pos += 3; + } else if ( 4<=lenMax + && 0xF0 == (_input[pos+0] & 0xF8) + && 0x80 == (_input[pos+1] & 0xC0) + && 0x80 == (_input[pos+2] & 0xC0) + && 0x80 == (_input[pos+3] & 0xC0)) { + tmpData[0] = _input[pos+0]; + tmpData[1] = _input[pos+1]; + tmpData[2] = _input[pos+2]; + tmpData[3] = _input[pos+3]; + tmpData[4] = '\0'; + pos += 4; + } else { + tmpData[0] = '\0'; + pos += 1; + } + out += etk::setUtf8(tmpData); } return out; } @@ -197,6 +209,20 @@ bool stobool(const std::string& _str) { return false; } +std::string std::to_string(bool _val) { + if (_val == true) { + return "true"; + } + return "false"; +} + +std::u32string to_u32string(bool _val) { + if (_val == true) { + return U"true"; + } + return U"false"; +} + bool compare_no_case(const std::u32string& _obj, const std::u32string& _val) { if (_val.size() != _obj.size()) { return false; diff --git a/etk/UString.h b/etk/UString.h index 8693894..3927928 100644 --- a/etk/UString.h +++ b/etk/UString.h @@ -286,7 +286,10 @@ template std::u32string to_u32string(T t, std::ios_base & (*f)(std::ios oss << f << t; return to_u32string(oss.str()); } - +namespace std { + std::string to_string(bool _val); +} +std::u32string to_u32string(bool _val); std::u32string to_u32string(int _val); std::u32string to_u32string(long _val); std::u32string to_u32string(long long _val); diff --git a/etk/debug.cpp b/etk/debug.cpp index 8952300..5402efc 100644 --- a/etk/debug.cpp +++ b/etk/debug.cpp @@ -8,4 +8,4 @@ #include -const char * etkLibName = "Etk "; +const char * etkLibName = "etk "; diff --git a/etk/os/FSNode.cpp b/etk/os/FSNode.cpp index 141c4e1..df28541 100644 --- a/etk/os/FSNode.cpp +++ b/etk/os/FSNode.cpp @@ -38,9 +38,12 @@ extern "C" { std::string etk::simplifyPath(std::string _input) { // step 1 : for windows change \ in /: - TK_DEBUG("Siplify(1) : \"" << _input << "\""); + TK_DBG_MODE("Siplify(1) : \"" << _input << "\""); size_t currentPos = 0; - while(currentPos <= _input.size()) { + if (_input.size() == 0) { + return _input; + } + while(currentPos < _input.size()) { if (_input[currentPos] == '\\') { _input[currentPos] = '/'; } @@ -48,9 +51,12 @@ std::string etk::simplifyPath(std::string _input) { continue; } // step 2 : remove all '//' - TK_DEBUG("Siplify(2) : \"" << _input << "\""); + TK_DBG_MODE("Siplify(2) : \"" << _input << "\""); currentPos = 0; - while(currentPos <= _input.size()-1) { + if (_input.size() <= 1) { + return _input; + } + while(currentPos < _input.size()-1) { if ( _input[currentPos] != '/' || _input[currentPos+1] != '/') { currentPos++; @@ -59,10 +65,13 @@ std::string etk::simplifyPath(std::string _input) { _input.erase(currentPos, 1); } // step 3 remove xxx/.. - TK_DEBUG("Siplify(3) : \"" << _input << "\""); + TK_DBG_MODE("Siplify(3) : \"" << _input << "\""); size_t lastSlashPos = std::string::npos; currentPos = 0; - while(currentPos <= _input.size()-2) { + if (_input.size() <= 2) { + return _input; + } + while(currentPos < _input.size()-2) { if ( _input[currentPos] != '/' || _input[currentPos+1] != '.' || _input[currentPos+2] != '.') { @@ -81,7 +90,7 @@ std::string etk::simplifyPath(std::string _input) { lastSlashPos = std::string::npos; currentPos = 0; } - TK_DEBUG("Siplify(4) : \"" << _input << "\""); + TK_DBG_MODE("Siplify(4) : \"" << _input << "\""); return _input; }