inline void append_hex( std::basic_string<CharT,Traits,Allocator>& s, boost::uint8_t n, bool is_upper) { std::pair<CharT,CharT> hex = to_hex_pair<CharT>(n, is_upper); s.push_back(hex.first); s.push_back(hex.second); }
virtual typename base::int_type overflow(typename base::int_type __c = base::traits_type::eof()) { if (__c != base::traits_type::eof()) { int n = static_cast<int>(str_.size()); str_.push_back(static_cast<CharT>(__c)); str_.resize(str_.capacity()); base::setp(const_cast<CharT*>(str_.data()), const_cast<CharT*>(str_.data() + str_.size())); base::pbump(n+1); } return __c; }
std::basic_string < Elem, Traits > cell_encode(std::basic_string < Elem, Traits > Str, Elem Sep_, Elem Esc){ if(Str.find(Sep_) < Str.size() || Str.find(Esc) < Str.size()){ for(auto itr = Str.begin(); itr != Str.end(); ++itr){ if(*itr == Esc){ itr = Str.insert(++itr, Esc); } } Str.insert(Str.begin(), Esc); Str.push_back(Esc); } return std::move(Str); }
virtual typename base::int_type overflow(typename base::int_type ch = base::traits_type::eof()) { if (ch != base::traits_type::eof()) { std::size_t n = str_.size(); str_.push_back(static_cast<CharT>(ch)); str_.resize(str_.capacity()); base::setp(const_cast<CharT*>(str_.data()), const_cast<CharT*>(str_.data() + str_.size())); base::pbump(static_cast<int>(n+1)); } return ch; }
void unescape_wide_string( const std::string &src, std::basic_string<unsigned int> &dest) { dest.reserve(src.size()); // about that long, but may be shorter for(unsigned i=0; i<src.size(); i++) { unsigned int ch=(unsigned char)src[i]; if(ch=='\\') // escape? { i++; assert(i<src.size()); // backslash can't be last character ch=(unsigned char)src[i]; switch(ch) { case '\\': dest.push_back(ch); break; case 'n': dest.push_back('\n'); break; /* NL (0x0a) */ case 't': dest.push_back('\t'); break; /* HT (0x09) */ case 'v': dest.push_back('\v'); break; /* VT (0x0b) */ case 'b': dest.push_back('\b'); break; /* BS (0x08) */ case 'r': dest.push_back('\r'); break; /* CR (0x0d) */ case 'f': dest.push_back('\f'); break; /* FF (0x0c) */ case 'a': dest.push_back('\a'); break; /* BEL (0x07) */ case '"': dest.push_back('"'); break; case '\'': dest.push_back('\''); break; case 'u': // universal character case 'U': // universal character i++; { std::string hex; unsigned count=(ch=='u')?4:8; hex.reserve(count); for(; count!=0 && i<src.size(); i++, count--) hex+=src[i]; // go back i--; unsigned int result; sscanf(hex.c_str(), "%x", &result); ch=result; } dest.push_back(ch); break; case 'x': // hex i++; { std::string hex; while(i<src.size() && isxdigit(src[i])) { hex+=src[i]; i++; } // go back i--; unsigned int result; sscanf(hex.c_str(), "%x", &result); ch=result; } dest.push_back(ch); break; default: if(isdigit(ch)) // octal { std::string octal; while(i<src.size() && isdigit(src[i])) { octal+=src[i]; i++; } // go back i--; unsigned int result; sscanf(octal.c_str(), "%o", &result); ch=result; dest.push_back(ch); } else { // Unknown escape sequence. // Both GCC and CL turn \% into %. dest.push_back(ch); } } } else dest.push_back(ch); } }
bool join_sub_string ( const basic_cstring_view<CharT, Traits>& in, typename basic_cstring_view<CharT, Traits>::size_type& pos, std::basic_string<CharT, Traits, Allocator>& out, InputIterator valuesBegin, InputIterator valuesEnd, bool optional ) { using string_type = std::basic_string<CharT, Traits, Allocator>; using size_type = typename string_type::size_type; bool escapeChar = false; bool replacedAllValues = true; const auto numValues = static_cast<size_type>(std::distance(valuesBegin, valuesEnd)); if (numValues < 0) throw std::invalid_argument("reversed range iterators in join_string"); for (auto num = in.size(); pos < num;) { /* Get next character */ auto c = in[pos++]; if (escapeChar) { /* Add character without transformation to output string */ out.push_back(c); escapeChar = false; } else { if (c == CharT('\\')) { /* Next character will be added without transformation */ escapeChar = true; } else if (c == CharT('{')) { /* Parse index N in '{N}' */ string_type idxStr; while (pos < num) { /* Get next character */ c = in[pos++]; if (c == CharT('}')) break; else idxStr.push_back(c); } /* Get value by index from array */ const auto idx = static_cast<size_type>(std::stoul(idxStr)); if (idx < numValues) { /* Append value to output string */ const auto& val = *(valuesBegin + idx); if (string_empty(val)) replacedAllValues = false; else string_append(out, val); } else if (optional) { /* This sub string will not be added to the final output string */ replacedAllValues = false; } else { /* If this value replacement was not optional -> error */ throw std::out_of_range( "index (" + std::to_string(idx) + ") out of range [0, " + std::to_string(numValues) + ") in join_string" ); } } else if (c == CharT('[')) { /* Parse optional part with recursive call */ string_type outOpt; if (join_sub_string(in, pos, outOpt, valuesBegin, valuesEnd, true)) out.append(outOpt); } else if (c == CharT(']')) { /* Close optional part and return from recursive call */ break; } else { /* Add current character to output string */ out.push_back(c); } } } if (escapeChar) throw std::invalid_argument("incomplete escape character in report string"); return replacedAllValues; }
bool enum_file_lines::GetTString(std::vector<T>& From, std::basic_string<T>& To, eol::type& Eol, bool bBigEndian) const { To.clear(); // Обработка ситуации, когда у нас пришёл двойной \r\r, а потом не было \n. // В этом случаем считаем \r\r двумя MAC окончаниями строк. if (m_CrCr) { m_CrCr = false; Eol = eol::type::mac; return true; } auto CurrentEol = eol::type::none; for (const auto* ReadBufPtr = ReadPos < ReadSize? From.data() + ReadPos / sizeof(T) : nullptr; ; ++ReadBufPtr, ReadPos += sizeof(T)) { if (ReadPos >= ReadSize) { if (!(SrcFile.Read(From.data(), ReadBufCount * sizeof(T), ReadSize) && ReadSize)) { Eol = CurrentEol; return !To.empty() || CurrentEol != eol::type::none; } if (bBigEndian && sizeof(T) != 1) { swap_bytes(From.data(), From.data(), ReadSize); } ReadPos = 0; ReadBufPtr = From.data(); } if (CurrentEol == eol::type::none) { // UNIX if (*ReadBufPtr == m_Eol.lf<T>()) { CurrentEol = eol::type::unix; continue; } // MAC / Windows? / Notepad? else if (*ReadBufPtr == m_Eol.cr<T>()) { CurrentEol = eol::type::mac; continue; } } else if (CurrentEol == eol::type::mac) { if (m_CrSeen) { m_CrSeen = false; // Notepad if (*ReadBufPtr == m_Eol.lf<T>()) { CurrentEol = eol::type::bad_win; continue; } else { // Пришёл \r\r, а \n не пришёл, поэтому считаем \r\r двумя MAC окончаниями строк m_CrCr = true; break; } } else { // Windows if (*ReadBufPtr == m_Eol.lf<T>()) { CurrentEol = eol::type::win; continue; } // Notepad or two MACs? else if (*ReadBufPtr == m_Eol.cr<T>()) { m_CrSeen = true; continue; } else { break; } } } else { break; } To.push_back(*ReadBufPtr); CurrentEol = eol::type::none; } Eol = CurrentEol; return true; }