Commit c2a285ae authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Extract strlcpy to a new c-string lib

Summary:
[Folly] Extract `strlcpy` to a new c-string lib in `folly/lang/CString.h`.

This new lib would be for routines that should but don't appear in `<cstring>`.

Reviewed By: marcinpe

Differential Revision: D18032925

fbshipit-source-id: 746851f68e86a5ec2bea5cc659c19c4fe2f0f02a
parent b440ee45
......@@ -737,6 +737,7 @@ if (BUILD_TESTS)
DIRECTORY lang/test/
TEST aligned_test SOURCES AlignedTest.cpp
TEST bits_test SOURCES BitsTest.cpp
TEST c_string_test SOURCES CStringTest.cpp
TEST cast_test SOURCES CastTest.cpp
TEST cold_class_test SOURCES ColdClassTest.cpp
TEST safe_assert_test SOURCES SafeAssertTest.cpp
......
......@@ -20,6 +20,7 @@
#include <cstring>
#include <folly/detail/Demangle.h>
#include <folly/lang/CString.h>
#include <folly/portability/Config.h>
#if FOLLY_DETAIL_HAVE_DEMANGLE_H
......@@ -118,14 +119,4 @@ size_t demangle(const char* name, char* out, size_t outSize) {
#endif
size_t strlcpy(char* dest, const char* const src, size_t size) {
size_t len = strlen(src);
if (size != 0) {
size_t n = std::min(len, size - 1); // always null terminate!
memcpy(dest, src, n);
dest[n] = '\0';
}
return len;
}
} // namespace folly
......@@ -59,7 +59,4 @@ inline size_t demangle(const std::type_info& type, char* buf, size_t bufSize) {
return demangle(type.name(), buf, bufSize);
}
// glibc doesn't have strlcpy
size_t strlcpy(char* dest, const char* const src, size_t size);
} // namespace folly
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/lang/CString.h>
#include <algorithm>
namespace folly {
std::size_t
strlcpy(char* const dest, char const* const src, std::size_t const size) {
std::size_t const len = std::strlen(src);
if (size != 0) {
std::size_t const n = std::min(len, size - 1); // always null terminate!
std::memcpy(dest, src, n);
dest[n] = '\0';
}
return len;
}
} // namespace folly
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cstddef>
#include <cstring>
namespace folly {
// strlcpy
//
// mimic: strlcpy, libbsd
std::size_t strlcpy(char* dest, char const* src, std::size_t size);
} // namespace folly
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/lang/CString.h>
#include <folly/portability/GTest.h>
class CStringTest : public testing::Test {};
TEST_F(CStringTest, strlcpy) {
char buf[6];
EXPECT_EQ(3, folly::strlcpy(buf, "abc", 6));
EXPECT_EQ('\0', buf[3]);
EXPECT_EQ("abc", std::string(buf));
EXPECT_EQ(7, folly::strlcpy(buf, "abcdefg", 3));
EXPECT_EQ('\0', buf[2]);
EXPECT_EQ("ab", std::string(buf));
const char* big_string = "abcdefghijklmnop";
EXPECT_EQ(strlen(big_string), folly::strlcpy(buf, big_string, sizeof(buf)));
EXPECT_EQ('\0', buf[5]);
EXPECT_EQ("abcde", std::string(buf));
buf[0] = 'z';
EXPECT_EQ(strlen(big_string), folly::strlcpy(buf, big_string, 0));
EXPECT_EQ('z', buf[0]); // unchanged, size = 0
}
......@@ -17,6 +17,7 @@
#include <folly/Demangle.h>
#include <folly/detail/Demangle.h>
#include <folly/lang/CString.h>
#include <folly/portability/GTest.h>
using folly::demangle;
......@@ -85,25 +86,3 @@ TEST(Demangle, LongSymbolFallback) {
#endif // defined(FOLLY_DEMANGLE_MAX_SYMBOL_SIZE)
#endif // FOLLY_DETAIL_HAVE_DEMANGLE_H
TEST(Demangle, strlcpy) {
char buf[6];
EXPECT_EQ(3, folly::strlcpy(buf, "abc", 6));
EXPECT_EQ('\0', buf[3]);
EXPECT_EQ("abc", std::string(buf));
EXPECT_EQ(7, folly::strlcpy(buf, "abcdefg", 3));
EXPECT_EQ('\0', buf[2]);
EXPECT_EQ("ab", std::string(buf));
const char* big_string = "abcdefghijklmnop";
EXPECT_EQ(strlen(big_string), folly::strlcpy(buf, big_string, sizeof(buf)));
EXPECT_EQ('\0', buf[5]);
EXPECT_EQ("abcde", std::string(buf));
buf[0] = 'z';
EXPECT_EQ(strlen(big_string), folly::strlcpy(buf, big_string, 0));
EXPECT_EQ('z', buf[0]); // unchanged, size = 0
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment