]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
chore: Touch up Util::read_text_file code
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 19 Feb 2022 15:24:36 +0000 (16:24 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 19 Feb 2022 15:24:36 +0000 (16:24 +0100)
src/Util.cpp
src/Util.hpp

index d9c5fdbf2fcc5363c92320eef3ea83e6ad7965e7..55254489bee1e22757da8b1afeca4e206a805c08 100644 (file)
@@ -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<const char16_t*>(result.c_str());
-    std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, 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<const char16_t*>(result.c_str());
+    std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
+    result = converter.to_bytes(result_as_u16);
+  }
+  return result;
+}
+
 string_view
 remove_extension(string_view path)
 {
index de191e7cb0d9b65a64fbf8566c414971a5b28b43..67cd164e43e6ce417aa5761aad5307bad47a903f 100644 (file)
@@ -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);