Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 905cf76

Browse files
committed
add reverse string sample code
1 parent 477960f commit 905cf76

File tree

9 files changed

+167
-17
lines changed

9 files changed

+167
-17
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cmake_minimum_required(VERSION 3.10)
22
project(hello_world VERSION 0.0.0)
33

4+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
5+
46
set(ENABLE_TESTING "Add the tests subdirectory to the build." OFF)
57

68
add_subdirectory(src)

conanfile.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,31 @@ class HelloWorldConan(ConanFile):
66
version = "0.0.0"
77
url = ""
88
settings = "os", "compiler", "build_type", "arch"
9-
#generators = {"cmake_find_package", "BazelDeps", "BazelToolchain"}
10-
generators = {"BazelDeps", "BazelToolchain"}
9+
generators = {"cmake_find_package"}
10+
# generators = {"BazelDeps", "BazelToolchain"}
1111
options = {"enable_testing":[True, False]}
1212
default_options = {"enable_testing": False}
1313
exports_sources = {"src/**", "tests/**", "CMakeLists.txt"}
1414

1515
def requirements(self):
1616
self.requires.add("boost/1.73.0")
17+
self.requires.add("ms-gsl/3.1.0")
1718
if self.options.enable_testing:
1819
self.requires.add("gtest/1.8.1")
1920

2021
def build(self):
21-
# cmake = CMake(self)
22-
# cmake.definitions["ENABLE_TESTING"] = "ON" if self.options.enable_testing else "OFF"
23-
# cmake.configure()
24-
# cmake.build()
25-
bazel = Bazel(self)
26-
bazel.configure()
27-
bazel.build(label="//...", args="--sandbox_debug")
22+
cmake = CMake(self)
23+
cmake.definitions["ENABLE_TESTING"] = "ON" if self.options.enable_testing else "OFF"
24+
cmake.configure()
25+
cmake.build()
26+
27+
28+
# bazel = Bazel(self)
29+
# bazel.configure()
30+
# # build command does not seem to be applying args yet
31+
# # need to configure toolchain to used c++17 or above
32+
# # https://docs.bazel.build/versions/main/tutorial/cc-toolchain-config.html#configuring-the-c-toolchain
33+
# bazel.build(label="//...", args="--config=clang_config")
2834

2935
def package(self):
3036
self.copy("*.hpp", dst="include/", src="src/include/")

src/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ cc_library(
66
name = "hello_world",
77
srcs = glob(["*.cpp"]),
88
hdrs = glob(["include/*.hpp"]),
9-
strip_include_prefix = "include"
9+
strip_include_prefix = "include",
10+
deps = ["@ms-gsl//:ms-gsl"]
1011
)

src/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
cmake_minimum_required(VERSION 3.10)
22

33
find_package(Boost 1.73.0 REQUIRED)
4+
find_package(Microsoft.GSL 3.1.0 REQUIRED)
45

56
add_library(hello_world
67
hello_world.cpp
8+
string_reversal.cpp
79
)
810

911
target_include_directories(hello_world PUBLIC
@@ -14,7 +16,8 @@ target_include_directories(hello_world PUBLIC
1416
target_compile_features(hello_world PUBLIC cxx_std_20)
1517

1618
target_link_libraries(hello_world PUBLIC
17-
Boost::Boost
19+
Boost::Boost
20+
Microsoft.GSL::Microsoft.GSL
1821
)
1922

2023
include(CheckIPOSupported)

src/include/string_reversal.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @file string_reversal.hpp
3+
* @date 2021-06-28
4+
* @brief Write a function that reverses a string.
5+
* The input string is given as an array of characters s.
6+
* @link https://leetcode.com/problems/reverse-string/
7+
* @note Would obviously just use std::reverse algorithm
8+
* https://en.cppreference.com/w/cpp/algorithm/reverse
9+
*/
10+
11+
#ifndef STRING_REVERSAL_HPP
12+
#define STRING_REVERSAL_HPP
13+
14+
#include <array>
15+
#include <string>
16+
17+
namespace string_reversal {
18+
19+
constexpr auto kPrintableAsciiCount = size_t{95};
20+
21+
constexpr std::array<char, kPrintableAsciiCount> GetPrintableAsciiCharacters();
22+
23+
bool IsPrintableAsciiCharacters(std::string& s);
24+
25+
/**
26+
* @brief Reverses a string of characters.
27+
* @pre 1 <= s.length <= 10^5
28+
* @pre s[i] is a printable ascii character. (this is more of an assertion)
29+
* @note Do not allocate extra space. Modify the input in-place with O(1) extra
30+
* memory.
31+
* @note Time complexity: O(N)
32+
* @note Space complexity: O(1)
33+
*/
34+
void ReverseString(std::string& s);
35+
36+
} /* namespace hello */
37+
38+
#endif /* STRING_REVERSAL_HPP */

src/string_reversal.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @file string_reversal.cpp
3+
* @date 2021-06-28
4+
*/
5+
6+
#include <string_reversal.hpp>
7+
8+
#include <gsl/gsl>
9+
10+
namespace string_reversal {
11+
12+
constexpr std::array<char, kPrintableAsciiCount> GetPrintableAsciiCharacters() {
13+
auto printable_ascii = std::array<char, kPrintableAsciiCount>{};
14+
auto index = size_t{0};
15+
// https://en.cppreference.com/w/cpp/language/ascii
16+
for (char i = ' '; i <= '~'; ++i) {
17+
printable_ascii[index] = i;
18+
index++;
19+
}
20+
return printable_ascii;
21+
}
22+
23+
constexpr std::array<char, kPrintableAsciiCount> kPrintableAsciiCharacters{
24+
GetPrintableAsciiCharacters()};
25+
26+
bool IsPrintableAsciiCharacters(std::string& s) {
27+
const auto printable_ascii = std::string{kPrintableAsciiCharacters.begin(),
28+
kPrintableAsciiCharacters.end()};
29+
return s.find_first_not_of(printable_ascii) == std::string_view::npos;
30+
}
31+
32+
void ReverseString(std::string& s) {
33+
Expects(s.length() >= 1 && s.length() <= 100000);
34+
Expects(IsPrintableAsciiCharacters(s));
35+
auto left = size_t{0};
36+
auto right = size_t{s.size() - 1};
37+
38+
while (left < right) {
39+
auto temp = s[left];
40+
s[left] = s[right];
41+
s[right] = temp;
42+
left++;
43+
right--;
44+
}
45+
}
46+
47+
} /* namespace hello */

tests/unit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ endif()
99

1010
add_executable(hello_world_unit
1111
src/hello_world_unit.cpp
12+
src/string_reversal_unit.cpp
1213
src/main.cpp
1314
)
1415

tests/unit/src/hello_world_unit.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
* @date 2020-07-01
55
*/
66

7-
#include <gtest/gtest.h>
7+
#include <gtest/gtest.h>
88

9-
#include <hello_world.hpp>
9+
#include <hello_world.hpp>
1010

11-
using namespace hello;
11+
using namespace hello;
1212

13-
TEST(HelloWorldUnit, GetHelloWorldString_ReturnsHelloWorld) {
14-
ASSERT_EQ("hello world", GetHelloWorldString());
15-
}
13+
TEST(HelloWorldUnit, GetHelloWorldString_ReturnsHelloWorld) {
14+
ASSERT_EQ("hello world", GetHelloWorldString());
15+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @file string_reversal_unit.cpp
3+
* @author Edwin Gendron
4+
* @date 2021-06-28
5+
*/
6+
7+
#include <gtest/gtest.h>
8+
9+
#include <string_reversal.hpp>
10+
11+
using namespace string_reversal;
12+
13+
/**
14+
* @pre empty string
15+
* @post program terminated
16+
*/
17+
TEST(StringReversalUnit, ReverseStringEmptyStringExceptionThrown) {
18+
auto empty_string = std::string{};
19+
ASSERT_DEATH(ReverseString(empty_string), "");
20+
}
21+
22+
/**
23+
* @pre string of size 1
24+
* @post string remains the same
25+
*/
26+
TEST(StringReversalUnit, ReverseStringStringSizeOneExceptionThrown) {
27+
auto test_string = std::string{"a"};
28+
auto temp_test_string = std::string{test_string};
29+
ReverseString(test_string);
30+
ASSERT_EQ(temp_test_string, test_string);
31+
}
32+
33+
/**
34+
* @pre string of size 100005, 5 chars too large.
35+
* @post program terminated
36+
*/
37+
TEST(StringReversalUnit, ReverseStringStringSizeTooLargeExceptionThrown) {
38+
auto test_string = std::string(100005, 'a');
39+
ASSERT_DEATH(ReverseString(test_string), "");
40+
}
41+
42+
/**
43+
* @pre string to reverse within the specified size and larger than 1 char
44+
* @post string is reversed
45+
*/
46+
TEST(StringReversalUnit, ReverseStringStringSizeAcceptableAndGreaterThanOneReversedStringReturned) {
47+
auto test_string = std::string{"abcdefghijklmnopqrstuvwxyz"};
48+
const auto expected_reversed_result =
49+
std::string{"zyxwvutsrqponmlkjihgfedcba"};
50+
ReverseString(test_string);
51+
ASSERT_EQ(test_string, expected_reversed_result);
52+
}

0 commit comments

Comments
 (0)