Skip to content

Commit 38459f1

Browse files
committed
refactor: Remove locale-dependent function calls
Changes suggested in bitcoin/bitcoin#31741 (comment) to be able to enable more linters in Bitcoin Core CI.
1 parent 9119300 commit 38459f1

File tree

6 files changed

+21
-8
lines changed

6 files changed

+21
-8
lines changed

example/calculator.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <calculator.h>
6+
#include <charconv>
67
#include <fstream>
78
#include <init.capnp.h>
89
#include <init.capnp.proxy.h> // NOLINT(misc-include-cleaner)
@@ -44,8 +45,12 @@ int main(int argc, char** argv)
4445
std::cout << "Usage: mpcalculator <fd>\n";
4546
return 1;
4647
}
48+
int fd;
49+
if (std::from_chars(argv[1], argv[1] + strlen(argv[1]), fd).ec != std::errc{}) {
50+
std::cout << argv[1] << " is not a number or is larger than an int\n";
51+
return 1;
52+
}
4753
mp::EventLoop loop("mpcalculator", LogPrint);
48-
const int fd = std::stoi(argv[1]);
4954
std::unique_ptr<Init> init = std::make_unique<InitImpl>();
5055
mp::ServeStream<InitInterface>(loop, fd, *init);
5156
loop.loop();

example/example.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static auto Spawn(mp::EventLoop& loop, const std::string& process_argv0, const s
2525
fs::path path = process_argv0;
2626
path.remove_filename();
2727
path.append(new_exe_name);
28-
return {path.string(), std::to_string(fd)};
28+
return {path.string(), std::format("{:d}", fd)};
2929
});
3030
return std::make_tuple(mp::ConnectStream<InitInterface>(loop, fd), pid);
3131
}

example/printer.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <fstream>
6+
#include <charconv>
67
#include <init.capnp.h>
78
#include <init.capnp.proxy.h> // NOLINT(misc-include-cleaner)
89
#include <init.h>
@@ -37,8 +38,12 @@ int main(int argc, char** argv)
3738
std::cout << "Usage: mpprinter <fd>\n";
3839
return 1;
3940
}
41+
int fd;
42+
if (std::from_chars(argv[1], argv[1] + strlen(argv[1]), fd).ec != std::errc{}) {
43+
std::cout << argv[1] << " is not a number or is larger than an int\n";
44+
return 1;
45+
}
4046
mp::EventLoop loop("mpprinter", LogPrint);
41-
const int fd = std::stoi(argv[1]);
4247
std::unique_ptr<Init> init = std::make_unique<InitImpl>();
4348
mp::ServeStream<InitInterface>(loop, fd, *init);
4449
loop.loop();

src/mp/gen.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <errno.h>
1414
#include <fstream>
1515
#include <functional>
16+
#include <iostream>
1617
#include <kj/array.h>
1718
#include <kj/common.h>
1819
#include <kj/filesystem.h>
@@ -637,7 +638,7 @@ static void Generate(kj::StringPtr src_prefix,
637638
int main(int argc, char** argv)
638639
{
639640
if (argc < 3) {
640-
fprintf(stderr, "Usage: " PROXY_BIN " SRC_PREFIX INCLUDE_PREFIX SRC_FILE [IMPORT_PATH...]\n");
641+
std::cerr << "Usage: " << PROXY_BIN << " SRC_PREFIX INCLUDE_PREFIX SRC_FILE [IMPORT_PATH...]\n";
641642
exit(1);
642643
}
643644
std::vector<kj::StringPtr> import_paths;

src/mp/util.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <mp/util.h>
77

88
#include <errno.h>
9+
#include <format>
910
#include <kj/common.h>
1011
#include <kj/string-tree.h>
1112
#include <pthread.h>
@@ -85,9 +86,7 @@ std::string LogEscape(const kj::StringTree& string)
8586
if (c == '\\') {
8687
result.append("\\\\");
8788
} else if (c < 0x20 || c > 0x7e) {
88-
char escape[4];
89-
snprintf(escape, 4, "\\%02x", c);
90-
result.append(escape);
89+
result.append(std::format("\\{:02x}", c));
9190
} else {
9291
result.push_back(c);
9392
}

test/mp/test/test.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <cstdio>
1212
#include <future>
1313
#include <functional>
14+
#include <iostream>
1415
#include <memory>
1516
#include <kj/common.h>
1617
#include <kj/memory.h>
@@ -27,7 +28,9 @@ KJ_TEST("Call FooInterface methods")
2728
std::promise<std::unique_ptr<ProxyClient<messages::FooInterface>>> foo_promise;
2829
std::function<void()> disconnect_client;
2930
std::thread thread([&]() {
30-
EventLoop loop("mptest", [](bool raise, const std::string& log) { printf("LOG%i: %s\n", raise, log.c_str()); });
31+
EventLoop loop("mptest", [](bool raise, const std::string& log) {
32+
std::cout << "LOG" << raise << ": " << log << "\n";
33+
});
3134
auto pipe = loop.m_io_context.provider->newTwoWayPipe();
3235

3336
auto connection_client = std::make_unique<Connection>(loop, kj::mv(pipe.ends[0]));

0 commit comments

Comments
 (0)