Skip to content

Commit

Permalink
src: refactor to use locale functions
Browse files Browse the repository at this point in the history
This makes the code more readable.

Signed-off-by: Darshan Sen <[email protected]>

PR-URL: #39014
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
RaisinTen authored and jasnell committed Jun 14, 2021
1 parent 68229a9 commit 940f2c2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/inspector/node_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ String StringViewToUtf8(v8_inspector::StringView view) {

String fromDouble(double d) {
std::ostringstream stream;
stream.imbue(std::locale("C")); // Ignore locale
stream.imbue(std::locale::classic()); // Ignore current locale
stream << d;
return stream.str();
}

double toDouble(const char* buffer, size_t length, bool* ok) {
std::istringstream stream(std::string(buffer, length));
stream.imbue(std::locale("C")); // Ignore locale
stream.imbue(std::locale::classic()); // Ignore current locale
double d;
stream >> d;
*ok = !stream.fail();
Expand Down
2 changes: 1 addition & 1 deletion src/tracing/traced_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ std::string DoubleToCString(double v) {
default:
// This is a far less sophisticated version than the one used inside v8.
std::ostringstream stream;
stream.imbue(std::locale("C")); // Ignore locale
stream.imbue(std::locale::classic()); // Ignore current locale
stream << v;
return stream.str();
}
Expand Down
5 changes: 3 additions & 2 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <cmath>
#include <cstring>
#include <locale>
#include "util.h"

// These are defined by <sys/byteorder.h> or <netinet/in.h> on some systems.
Expand Down Expand Up @@ -274,7 +275,7 @@ void SwapBytes64(char* data, size_t nbytes) {
}

char ToLower(char c) {
return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
return std::tolower(c, std::locale::classic());
}

std::string ToLower(const std::string& in) {
Expand All @@ -285,7 +286,7 @@ std::string ToLower(const std::string& in) {
}

char ToUpper(char c) {
return c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c;
return std::toupper(c, std::locale::classic());
}

std::string ToUpper(const std::string& in) {
Expand Down

0 comments on commit 940f2c2

Please sign in to comment.