From: Joel Rosdahl Date: Sat, 19 Feb 2022 15:24:36 +0000 (+0100) Subject: chore: Touch up Util::read_text_file code X-Git-Tag: v4.6~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22a87e468e721649a622ee894e692963d5d4114c;p=thirdparty%2Fccache.git chore: Touch up Util::read_text_file code --- diff --git a/src/Util.cpp b/src/Util.cpp index d9c5fdbf2..55254489b 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2021 Joel Rosdahl and other contributors +// Copyright (C) 2019-2022 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -1088,21 +1088,6 @@ read_file(const std::string& path, size_t size_hint) return result; } -std::string -read_text_file(const std::string& path, size_t size_hint) -{ - std::string result = read_file(path, size_hint); - // Convert to UTF-8 if the contents start with UTF-16 little-endian BOM - if (has_utf16_le_bom(result)) { - result.erase(0, 2); // Remove BOM - std::u16string result_as_u16((result.size() / 2) + 1, '\0'); - result_as_u16 = reinterpret_cast(result.c_str()); - std::wstring_convert, char16_t> converter; - result = converter.to_bytes(result_as_u16); - } - return result; -} - #ifndef _WIN32 std::string read_link(const std::string& path) @@ -1159,6 +1144,21 @@ real_path(const std::string& path, bool return_empty_on_error) return resolved ? resolved : (return_empty_on_error ? "" : path); } +std::string +read_text_file(const std::string& path, size_t size_hint) +{ + std::string result = read_file(path, size_hint); + // Convert to UTF-8 if the content starts with a UTF-16 little-endian BOM. + if (has_utf16_le_bom(result)) { + result.erase(0, 2); // Remove BOM. + std::u16string result_as_u16((result.size() / 2) + 1, '\0'); + result_as_u16 = reinterpret_cast(result.c_str()); + std::wstring_convert, char16_t> converter; + result = converter.to_bytes(result_as_u16); + } + return result; +} + string_view remove_extension(string_view path) { diff --git a/src/Util.hpp b/src/Util.hpp index de191e7cb..67cd164e4 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2021 Joel Rosdahl and other contributors +// Copyright (C) 2019-2022 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -293,12 +293,6 @@ bool read_fd(int fd, DataReceiver data_receiver); // without the path. std::string read_file(const std::string& path, size_t size_hint = 0); -// Return contents of a text file as UTF-8 encoded string. -// -// Throws `core::Error` on error. The description contains the error message -// without the path. -std::string read_text_file(const std::string& path, size_t size_hint = 0); - #ifndef _WIN32 // Like readlink(2) but returns the string (or the empty string on failure). std::string read_link(const std::string& path); @@ -310,6 +304,12 @@ std::string read_link(const std::string& path); std::string real_path(const std::string& path, bool return_empty_on_error = false); +// Return contents of a text file as a UTF-8 encoded string. +// +// Throws `core::Error` on error. The description contains the error message +// without the path. +std::string read_text_file(const std::string& path, size_t size_hint = 0); + // Return a view into `path` containing the given path without the filename // extension as determined by `get_extension()`. nonstd::string_view remove_extension(nonstd::string_view path);