vector<int> readIntegerFile(string_view fileName) { ifstream inputStream(fileName.data()); if (inputStream.fail()) { // We failed to open the file: throw an exception const string error = "Unable to open file "s + fileName.data(); throw invalid_argument(error); } // Read the integers one-by-one and add them to a vector vector<int> integers; int temp; while (inputStream >> temp) { integers.push_back(temp); } if (!inputStream.eof()) { // We did not reach the end-of-file. // This means that some error occurred while reading the file. // Throw an exception. const string error = "Unable to read file "s + fileName.data(); throw runtime_error(error); } return integers; }
bdecode_node bdecode_node::dict_find(string_view key) const { TORRENT_ASSERT(type() == dict_t); bdecode_token const* tokens = m_root_tokens; // this is the first item int token = m_token_idx + 1; while (tokens[token].type != bdecode_token::end) { bdecode_token const& t = tokens[token]; TORRENT_ASSERT(t.type == bdecode_token::string); int const size = m_root_tokens[token + 1].offset - t.offset - t.start_offset(); if (int(key.size()) == size && std::equal(key.data(), key.data() + size, m_buffer + t.offset + t.start_offset())) { // skip key token += t.next_item; TORRENT_ASSERT(tokens[token].type != bdecode_token::end); return bdecode_node(tokens, m_buffer, m_buffer_size, token); } // skip key token += t.next_item; TORRENT_ASSERT(tokens[token].type != bdecode_token::end); // skip value token += tokens[token].next_item; } return bdecode_node(); }
int reload(lua_State* L, const Data& chunk, const string_view& name) { lua_getglobal(L, "package"); lua_pushliteral(L, "loaded"); lua_rawget(L, -2); R_ASSERT(lua_istable(L, -1), "Missing control table 'package.loaded'"); lua_pushlstring(L, name.data(), name.length()); lua_pushnil(L); lua_rawset(L, -3); lua_pop(L, 2); return load(L, chunk, name.data(), true); }
// returns the unicode codepoint and the number of bytes of the utf8 sequence // that was parsed. The codepoint is -1 if it's invalid std::pair<std::int32_t, int> parse_utf8_codepoint(string_view str) { int const sequence_len = trailingBytesForUTF8[static_cast<std::uint8_t>(str[0])] + 1; if (sequence_len > int(str.size())) return std::make_pair(-1, static_cast<int>(str.size())); if (sequence_len > 4) { return std::make_pair(-1, sequence_len); } if (!isLegalUTF8(reinterpret_cast<UTF8 const*>(str.data()), sequence_len)) { return std::make_pair(-1, sequence_len); } std::uint32_t ch = 0; for (int i = 0; i < sequence_len; ++i) { ch <<= 6; ch += static_cast<std::uint8_t>(str[static_cast<std::size_t>(i)]); } ch -= offsetsFromUTF8[sequence_len-1]; if (ch > 0x7fffffff) { return std::make_pair(-1, sequence_len); } return std::make_pair(static_cast<std::int32_t>(ch), sequence_len); }
void stdLogger(int level, string_view msg) noexcept { const auto now = UnixClock::now(); const auto t = UnixClock::to_time_t(now); const auto ms = timeToMs(now); struct tm tm; localtime_r(&t, &tm); // The following format has an upper-bound of 42 characters: // "%b %d %H:%M:%S.%03d %-7s [%d]: " // // Example: // Mar 14 00:00:00.000 WARNING [0123456789]: msg... // <----------------------------------------> char head[42 + 1]; size_t hlen = strftime(head, sizeof(head), "%b %d %H:%M:%S", &tm); hlen += sprintf(head + hlen, ".%03d %-7s [%d]: ", static_cast<int>(ms % 1000), logLabel(level), static_cast<int>(getpid())); char tail = '\n'; iovec iov[] = { {head, hlen}, // {const_cast<char*>(msg.data()), msg.size()}, // {&tail, 1} // }; int fd{level > LogWarning ? STDOUT_FILENO : STDERR_FILENO}; // Best effort given that this is the logger. #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-result" writev(fd, iov, sizeof(iov) / sizeof(iov[0])); #pragma GCC diagnostic pop }
void test_strview_basics(const string_view& sv, const char *p, size_t n) { ASSERT_EQ(p, sv.data()); ASSERT_EQ(n, sv.size()); ASSERT_EQ(n, sv.length()); ASSERT_EQ((n == 0), sv.empty()); ASSERT_EQ(p, sv.cbegin()); ASSERT_EQ(p, sv.begin()); ASSERT_EQ(p + n, sv.cend()); ASSERT_EQ(p + n, sv.end()); using reviter_t = std::reverse_iterator<string_view::const_iterator>; ASSERT_EQ(reviter_t(sv.end()), sv.rbegin()); ASSERT_EQ(reviter_t(sv.begin()), sv.rend()); ASSERT_EQ(reviter_t(sv.cend()), sv.crbegin()); ASSERT_EQ(reviter_t(sv.cbegin()), sv.crend()); for (size_t i = 0; i < n; ++i) { ASSERT_EQ(p[i], sv[i]); ASSERT_EQ(p[i], sv.at(i)); } ASSERT_THROW(sv.at(n), std::out_of_range); ASSERT_THROW(sv.at(string_view::npos), std::out_of_range); if (n > 0) { ASSERT_EQ(p, &(sv.front())); ASSERT_EQ(p + (n-1), &(sv.back())); ASSERT_EQ(p[0], sv.front()); ASSERT_EQ(p[n-1], sv.back()); } }
void write_buffer(DynamicBuffer& b, string_view s) { b.commit(boost::asio::buffer_copy( b.prepare(s.size()), boost::asio::buffer( s.data(), s.size()))); }
Exception::Exception(string_view what) noexcept { const auto len = min(what.size(), MaxErrMsg); if (len > 0) { memcpy(what_, what.data(), len); } what_[len] = '\0'; }
allocation_slot stack_allocator::copy_string(string_view str) { int const ret = int(m_storage.size()); m_storage.resize(ret + numeric_cast<int>(str.size()) + 1); std::memcpy(&m_storage[ret], str.data(), str.size()); m_storage[ret + int(str.length())] = '\0'; return allocation_slot(ret); }
path_match_result path_match(string_view input, string_view& match_contents) { if (input.length() < 2) return path_match_result::invalid; match_result result; token_kind kind; std::size_t length; switch (input.at(0)) { case '.': result = match_simple_string(input.data() + 1, input.data() + input.size(), kind, length); if (result == match_result::complete) { match_contents = input.substr(0, length + 1); return path_match_result::simple_object; } else { return path_match_result::invalid; } case '[': result = attempt_match(input.data() + 1, input.data() + input.length(), kind, length); if (result == match_result::complete) { if (input.length() == length + 1 || input.at(1 + length) != ']') return path_match_result::invalid; if (kind != token_kind::string && kind != token_kind::number) return path_match_result::invalid; match_contents = input.substr(0, length + 2); return path_match_result::brace; } else { return path_match_result::invalid; } default: return path_match_result::invalid; } }
std::wstring utf8_wchar(string_view utf8, error_code& ec) { // allocate space for worst-case std::wstring wide; wide.resize(utf8.size()); char const* src_start = utf8.data(); utf8_errors::error_code_enum const ret = convert_to_wide<sizeof(wchar_t)>::convert( &src_start, src_start + utf8.size(), wide); if (ret != utf8_errors::error_code_enum::conversion_ok) ec = make_error_code(ret); return wide; }
void ArticleCitations::readFile(string_view fileName) { // Open the file and check for failure. ifstream inputFile(fileName.data()); if (inputFile.fail()) { throw invalid_argument("Unable to open file"); } // Read the article author, title, etc. line. getline(inputFile, mArticle); // Skip the white space before the citations start. inputFile >> ws; int count = 0; // Save the current position so we can return to it. streampos citationsStart = inputFile.tellg(); // First count the number of citations. cout << "readFile(): counting number of citations" << endl; while (!inputFile.eof()) { // Skip white space before the next entry. inputFile >> ws; string temp; getline(inputFile, temp); if (!temp.empty()) { cout << "Citation " << count << ": " << temp << endl; count++; } } cout << "Found " << count << " citations" << endl; cout << "readFile(): reading citations" << endl; if (count != 0) { // Allocate an array of strings to store the citations. mCitations = new string[count]; mNumCitations = count; // Seek back to the start of the citations. inputFile.seekg(citationsStart); // Read each citation and store it in the new array. for (count = 0; count < mNumCitations; count++) { string temp; getline(inputFile, temp); if (!temp.empty()) { cout << temp << endl; mCitations[count] = temp; } } } else { mNumCitations = -1; } cout << "readFile(): finished" << endl; }
// ----------------------------------------------------------------------------- // Shows the splash window with [message]. // If [progress] is true, the progress bar is displayed // ----------------------------------------------------------------------------- void UI::showSplash(string_view message, bool progress, wxWindow* parent) { if (!splash_enabled || !isMainThread()) return; if (!splash_window) { SplashWindow::init(); splash_window = std::make_unique<SplashWindow>(); } splash_window->show(wxString{ message.data(), message.size() }, progress, parent); }
vector<int> readIntegerFile(string_view fileName) { ifstream inputStream(fileName.data()); if (inputStream.fail()) { // We failed to open the file: throw an exception throw exception(); } // Read the integers one-by-one and add them to a vector vector<int> integers; int temp; while (inputStream >> temp) { integers.push_back(temp); } return integers; }
//=========================================================================== Count::Count(string_view path) { auto * base = path.data(); auto * ptr = base; auto * eptr = ptr + path.size(); char const * colon = nullptr; // first ':' char const * slash = nullptr; // last '/' or '\' char const * dot = nullptr; // last '.' for (; ptr != eptr; ++ptr) { switch(*ptr) { case '.': dot = ptr; break; case '/': case '\\': slash = ptr; break; case ':': if (!colon) colon = ptr; break; } } if (!slash) { m_rootLen = colon ? unsigned(colon - base + 1) : 0; m_dirLen = 0; } else { m_rootLen = (colon && colon < slash) ? unsigned(colon - base + 1) : 0; m_dirLen = unsigned(slash - base + 1 - m_rootLen); } if (!dot || unsigned(dot - base) <= m_rootLen + m_dirLen) { m_stemLen = unsigned(path.size() - m_rootLen - m_dirLen); m_extLen = 0; } else { m_stemLen = unsigned(dot - base - m_rootLen - m_dirLen); m_extLen = unsigned(eptr - dot); if (m_extLen == 1 && m_stemLen == 1 && dot[-1] == '.') { m_stemLen = 2; m_extLen = 0; } else if (!m_stemLen) { m_stemLen = m_extLen; m_extLen = 0; } } assert(m_extLen == path.size() - m_rootLen - m_dirLen - m_stemLen); }
// Reads the names from the file and populates the database. // The database is a vector of name/count pairs, storing the // number of times each name shows up in the raw data. NameDB::NameDB(string_view nameFile) { // Open the file and check for errors. ifstream inputFile(nameFile.data()); if (!inputFile) { throw invalid_argument("Unable to open file"); } // Read the names one at a time. string name; while (inputFile >> name) { // Look up the name in the database so far. if (nameExists(name)) { // If the name exists in the database, just increment the count. incrementNameCount(name); } else { // If the name doesn't yet exist, add it with a count of 1. addNewName(name); } } }
void sysLogger(int level, string_view msg) noexcept { int prio; switch (level) { case LogCrit: prio = LOG_CRIT; break; case LogError: prio = LOG_ERR; break; case LogWarning: prio = LOG_WARNING; break; case LogNotice: prio = LOG_NOTICE; break; case LogInfo: prio = LOG_INFO; break; default: prio = LOG_DEBUG; } syslog(prio, "%.*s", static_cast<int>(msg.size()), msg.data()); }
static string_view ProcessMetasymbol(string_view const CurStr, subst_data& SubstData, string& Out) { const auto append_with_escape = [EscapeAmpersands = SubstData.EscapeAmpersands](string& Destination, string_view const Str) { if (EscapeAmpersands && contains(Str, L"&"sv)) { string Escaped(Str); replace(Escaped, L"&"sv, L"&&"sv); append(Destination, Escaped); } else { append(Destination, Str); } }; if (const auto Tail = tokens::skip(CurStr, tokens::passive_panel)) { SubstData.PassivePanel = true; return Tail; } if (const auto Tail = tokens::skip(CurStr, tokens::active_panel)) { SubstData.PassivePanel = false; return Tail; } if (const auto Tail = tokens::skip(CurStr, tokens::exclamation)) { if (!starts_with(Tail, L'?')) { Out.push_back(L'!'); return Tail; } } if (const auto Tail = tokens::skip(CurStr, tokens::name_extension)) { if (!starts_with(Tail, L'?')) { append_with_escape(Out, SubstData.Default().Normal.Name); return Tail; } } if (const auto Tail = tokens::skip(CurStr, tokens::short_name)) { append_with_escape(Out, SubstData.Default().Short.NameOnly); return Tail; } const auto GetExtension = [](string_view const Name) { const auto Extension = PointToExt(Name); return Extension.empty()? Extension : Extension.substr(1); }; if (const auto Tail = tokens::skip(CurStr, tokens::short_extension)) { append_with_escape(Out, GetExtension(SubstData.Default().Short.Name)); return Tail; } if (const auto Tail = tokens::skip(CurStr, tokens::extension)) { append_with_escape(Out, GetExtension(SubstData.Default().Normal.Name)); return Tail; } const auto CollectNames = [&SubstData, &append_with_escape](string& Str, auto const Selector) { append_with_escape(Str, join(select(SubstData.Default().Panel->enum_selected(), Selector), L" "sv)); }; if (const auto Tail = tokens::skip(CurStr, tokens::short_list)) { if (!starts_with(Tail, L'?')) { CollectNames(Out, &os::fs::find_data::AlternateFileName); return Tail; } } if (const auto Tail = tokens::skip(CurStr, tokens::list)) { if (!starts_with(Tail, L'?')) { CollectNames(Out, [](const os::fs::find_data& Data) { return quote_space(Data.FileName); }); return Tail; } } const auto GetListName = [&Out, &append_with_escape](string_view const Tail, subst_data& Data, bool Short) { const auto ExclPos = Tail.find(L'!'); if (ExclPos == Tail.npos || starts_with(Tail.substr(ExclPos + 1), L'?')) return size_t{}; const auto Modifiers = Tail.substr(0, ExclPos); if (Data.ListNames) { string Str; if (Data.Default().Panel->MakeListFile(Str, Short, Modifiers)) { if (Short) Str = ConvertNameToShort(Str); append_with_escape(Out, Str); Data.ListNames->add(std::move(Str)); } } else { append(Out, L'!', Short? L'$' : L'@', Modifiers, L'!'); } return Modifiers.size() + 1; }; if (const auto Tail = tokens::skip(CurStr, tokens::list_file)) { if (const auto Offset = GetListName(Tail, SubstData, false)) return string_view(Tail).substr(Offset); } if (const auto Tail = tokens::skip(CurStr, tokens::short_list_file)) { if (const auto Offset = GetListName(Tail, SubstData, true)) return string_view(Tail).substr(Offset); } if (const auto Tail = tokens::skip(CurStr, tokens::short_name_extension)) { if (!starts_with(Tail, L'?')) { append_with_escape(Out, SubstData.Default().Short.Name); return Tail; } } if (const auto Tail = tokens::skip(CurStr, tokens::short_name_extension_safe)) { if (!starts_with(Tail, L'?')) { append_with_escape(Out, SubstData.Default().Short.Name); SubstData.PreserveLFN = true; return Tail; } } if (const auto Tail = tokens::skip(CurStr, tokens::current_drive)) { const auto CurDir = IsAbsolutePath(SubstData.This.Normal.Name)? SubstData.This.Normal.Name : SubstData.PassivePanel? SubstData.Another.Panel->GetCurDir() : SubstData.CmdDir; auto RootDir = GetPathRoot(CurDir); DeleteEndSlash(RootDir); append_with_escape(Out, RootDir); return Tail; } if (const auto Tail = tokens::skip(CurStr, tokens::description)) { Out += SubstData.Default().GetDescription(); return Tail; } const auto GetPath = [](string_view const Tail, const subst_data& Data, bool Short, bool Real) { // TODO: paths on plugin panels are ambiguous auto CurDir = Data.PassivePanel? Data.Another.Panel->GetCurDir() : Data.CmdDir; if (Real) CurDir = ConvertNameToReal(CurDir); if (Short) CurDir = ConvertNameToShort(CurDir); AddEndSlash(CurDir); return CurDir; }; if (const auto Tail = tokens::skip(CurStr, tokens::path)) { Out += GetPath(Tail, SubstData, false, false); return Tail; } if (const auto Tail = tokens::skip(CurStr, tokens::short_path)) { Out += GetPath(Tail, SubstData, true, false); return Tail; } if (const auto Tail = tokens::skip(CurStr, tokens::real_path)) { Out += GetPath(Tail, SubstData, false, true); return Tail; } if (const auto Tail = tokens::skip(CurStr, tokens::real_short_path)) { Out += GetPath(Tail, SubstData, true, true); return Tail; } // !?<title>?<init>! if (const auto Tail = tokens::skip(CurStr, tokens::input)) { auto SkipSize = SkipInputToken(CurStr); // if bad format string skip 1 char if (!SkipSize) SkipSize = 1; Out.append(CurStr.data(), SkipSize); return CurStr.substr(SkipSize); } if (const auto Tail = tokens::skip(CurStr, tokens::name)) { append(Out, PointToName(SubstData.Default().Normal.NameOnly)); return Tail; } return CurStr; }
double SpreadsheetCell::stringToDouble(string_view inString) { return strtod(inString.data(), nullptr); }
// ----------------------------------------------------------------------------- // Sets the splash window progress bar [message] // ----------------------------------------------------------------------------- void UI::setSplashProgressMessage(string_view message) { if (splash_window && isMainThread()) splash_window->setProgressMessage(wxString{ message.data(), message.size() }); }
string MBCSToMBCS(string_view sv, unsigned cp_src, unsigned cp_dst) { return sv.length() != 0 ? MBCSToMBCS(CheckNonnegativeScalar<int>( sv.length()), sv.data(), cp_src, cp_dst) : string(); }
wstring MBCSToWCS(string_view sv, unsigned cp) { return sv.length() != 0 ? MBCSToWCS(CheckNonnegativeScalar<int>( sv.length()), sv.data(), cp) : wstring(); }
void xml_parse(string_view input , std::function<void(int, string_view, string_view)> callback) { char const* p = input.data(); char const* end = input.data() + input.size(); for (;p != end; ++p) { char const* start = p; // look for tag start for (; p != end && *p != '<'; ++p); if (p != start) { callback(xml_string, {start, std::size_t(p - start)}, {}); } if (p == end) break; // skip '<' ++p; if (p != end && p + 8 < end && string_begins_no_case("![CDATA[", p)) { // CDATA. match '![CDATA[' p += 8; start = p; while (p != end && !string_begins_no_case("]]>", p - 2)) ++p; // parse error if (p == end) { callback(xml_parse_error, "unexpected end of file", {}); break; } callback(xml_string, {start, std::size_t(p - start - 2)}, {}); continue; } // parse the name of the tag. for (start = p; p != end && *p != '>' && !is_space(*p); ++p); char const* tag_name_end = p; // skip the attributes for now for (; p != end && *p != '>'; ++p); // parse error if (p == end) { callback(xml_parse_error, "unexpected end of file", {}); break; } TORRENT_ASSERT(*p == '>'); char const* tag_end = p; if (*start == '/') { ++start; callback(xml_end_tag, {start, std::size_t(tag_name_end - start)}, {}); } else if (*(p - 1) == '/') { callback(xml_empty_tag, {start, std::size_t(std::min(tag_name_end - start, p - start - 1))}, {}); tag_end = p - 1; } else if (*start == '?' && *(p - 1) == '?') { ++start; callback(xml_declaration_tag, {start, std::size_t(std::min(tag_name_end - start, p - start - 1))}, {}); tag_end = p - 1; } else if (start + 5 < p && std::memcmp(start, "!--", 3) == 0 && std::memcmp(p - 2, "--", 2) == 0) { start += 3; callback(xml_comment, {start, std::size_t(tag_name_end - start - 2)}, {}); continue; } else { callback(xml_start_tag, {start, std::size_t(tag_name_end - start)}, {}); } // parse attributes for (char const* i = tag_name_end; i < tag_end; ++i) { char const* val_start = nullptr; // find start of attribute name while (i != tag_end && is_space(*i)) ++i; if (i == tag_end) break; start = i; // find end of attribute name while (i != tag_end && *i != '=' && !is_space(*i)) ++i; std::size_t const name_len = std::size_t(i - start); // look for equality sign for (; i != tag_end && *i != '='; ++i); // no equality sign found. Report this as xml_tag_content // instead of a series of key value pairs if (i == tag_end) { callback(xml_tag_content, {start, std::size_t(i - start)}, {}); break; } ++i; while (i != tag_end && is_space(*i)) ++i; // check for parse error (values must be quoted) if (i == tag_end || (*i != '\'' && *i != '\"')) { callback(xml_parse_error, "unquoted attribute value", {}); break; } char quote = *i; ++i; val_start = i; for (; i != tag_end && *i != quote; ++i); // parse error (missing end quote) if (i == tag_end) { callback(xml_parse_error, "missing end quote on attribute", {}); break; } callback(xml_attribute, {start, name_len}, {val_start, std::size_t(i - val_start)}); } } }
// Adds a new name to the database. void NameDB::addNewName(string_view name) { mNames.push_back(make_pair(name.data(), 1)); }
static boost::asio::const_buffers_1 buf(string_view s) { return {s.data(), s.size()}; }