Skip to content

Commit 4ef9104

Browse files
committed
Switch env var APIs to std::string_view
1 parent 30989c1 commit 4ef9104

File tree

2 files changed

+17
-36
lines changed

2 files changed

+17
-36
lines changed

cpp/src/arrow/util/io_util.cc

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,32 +1758,30 @@ Status FileTruncate(int fd, const int64_t size) {
17581758
// Environment variables
17591759
//
17601760

1761-
Result<std::string> GetEnvVar(const char* name) {
1761+
Result<std::string> GetEnvVar(std::string_view name) {
17621762
#ifdef _WIN32
17631763
// On Windows, getenv() reads an early copy of the process' environment
17641764
// which doesn't get updated when SetEnvironmentVariable() is called.
17651765
constexpr int32_t bufsize = 2000;
17661766
char c_str[bufsize];
1767-
auto res = GetEnvironmentVariableA(name, c_str, bufsize);
1767+
auto res = GetEnvironmentVariableA(name.data(), c_str, bufsize);
17681768
if (res >= bufsize) {
17691769
return Status::CapacityError("environment variable value too long");
17701770
} else if (res == 0) {
17711771
return Status::KeyError("environment variable '", name, "'undefined");
17721772
}
17731773
return std::string(c_str);
17741774
#else
1775-
char* c_str = getenv(name);
1775+
char* c_str = getenv(name.data());
17761776
if (c_str == nullptr) {
17771777
return Status::KeyError("environment variable '", name, "'undefined");
17781778
}
17791779
return std::string(c_str);
17801780
#endif
17811781
}
17821782

1783-
Result<std::string> GetEnvVar(const std::string& name) { return GetEnvVar(name.c_str()); }
1784-
17851783
#ifdef _WIN32
1786-
Result<NativePathString> GetEnvVarNative(const std::string& name) {
1784+
Result<NativePathString> GetEnvVarNative(std::string_view name) {
17871785
NativePathString w_name;
17881786
constexpr int32_t bufsize = 2000;
17891787
wchar_t w_str[bufsize];
@@ -1798,57 +1796,46 @@ Result<NativePathString> GetEnvVarNative(const std::string& name) {
17981796
return NativePathString(w_str);
17991797
}
18001798

1801-
Result<NativePathString> GetEnvVarNative(const char* name) {
1802-
return GetEnvVarNative(std::string(name));
1803-
}
1804-
18051799
#else
18061800

1807-
Result<NativePathString> GetEnvVarNative(const std::string& name) {
1801+
Result<NativePathString> GetEnvVarNative(std::string_view name) {
18081802
return GetEnvVar(name);
18091803
}
18101804

1811-
Result<NativePathString> GetEnvVarNative(const char* name) { return GetEnvVar(name); }
18121805
#endif
18131806

1814-
Status SetEnvVar(const char* name, const char* value) {
1807+
Status SetEnvVar(std::string_view name, std::string_view value) {
18151808
#ifdef _WIN32
1816-
if (SetEnvironmentVariableA(name, value)) {
1809+
if (SetEnvironmentVariableA(name.data(), value.data())) {
18171810
return Status::OK();
18181811
} else {
18191812
return Status::Invalid("failed setting environment variable");
18201813
}
18211814
#else
1822-
if (setenv(name, value, 1) == 0) {
1815+
if (setenv(name.data(), value.data(), 1) == 0) {
18231816
return Status::OK();
18241817
} else {
18251818
return Status::Invalid("failed setting environment variable");
18261819
}
18271820
#endif
18281821
}
18291822

1830-
Status SetEnvVar(const std::string& name, const std::string& value) {
1831-
return SetEnvVar(name.c_str(), value.c_str());
1832-
}
1833-
1834-
Status DelEnvVar(const char* name) {
1823+
Status DelEnvVar(std::string_view name) {
18351824
#ifdef _WIN32
1836-
if (SetEnvironmentVariableA(name, nullptr)) {
1825+
if (SetEnvironmentVariableA(name.data(), nullptr)) {
18371826
return Status::OK();
18381827
} else {
18391828
return Status::Invalid("failed deleting environment variable");
18401829
}
18411830
#else
1842-
if (unsetenv(name) == 0) {
1831+
if (unsetenv(name.data()) == 0) {
18431832
return Status::OK();
18441833
} else {
18451834
return Status::Invalid("failed deleting environment variable");
18461835
}
18471836
#endif
18481837
}
18491838

1850-
Status DelEnvVar(const std::string& name) { return DelEnvVar(name.c_str()); }
1851-
18521839
//
18531840
// Temporary directories
18541841
//

cpp/src/arrow/util/io_util.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <memory>
2626
#include <optional>
2727
#include <string>
28+
#include <string_view>
2829
#include <utility>
2930
#include <vector>
3031

@@ -238,23 +239,16 @@ Status MemoryMapRemap(void* addr, size_t old_size, size_t new_size, int fildes,
238239
ARROW_EXPORT
239240
Status MemoryAdviseWillNeed(const std::vector<MemoryRegion>& regions);
240241

242+
// Returns KeyError if the environment variable doesn't exist
241243
ARROW_EXPORT
242-
Result<std::string> GetEnvVar(const char* name);
244+
Result<std::string> GetEnvVar(std::string_view name);
243245
ARROW_EXPORT
244-
Result<std::string> GetEnvVar(const std::string& name);
245-
ARROW_EXPORT
246-
Result<NativePathString> GetEnvVarNative(const char* name);
247-
ARROW_EXPORT
248-
Result<NativePathString> GetEnvVarNative(const std::string& name);
246+
Result<NativePathString> GetEnvVarNative(std::string_view name);
249247

250248
ARROW_EXPORT
251-
Status SetEnvVar(const char* name, const char* value);
252-
ARROW_EXPORT
253-
Status SetEnvVar(const std::string& name, const std::string& value);
254-
ARROW_EXPORT
255-
Status DelEnvVar(const char* name);
249+
Status SetEnvVar(std::string_view name, std::string_view value);
256250
ARROW_EXPORT
257-
Status DelEnvVar(const std::string& name);
251+
Status DelEnvVar(std::string_view name);
258252

259253
ARROW_EXPORT
260254
std::string ErrnoMessage(int errnum);

0 commit comments

Comments
 (0)