Skip to content

Commit ad62682

Browse files
author
Thomas Kiley
authored
Merge pull request diffblue#2071 from thk123/refactor/split-string-unit-tests
Enhance split_string to support splitting on white space [TG-2922]
2 parents 3e2ab6f + fc8ba88 commit ad62682

File tree

9 files changed

+265
-79
lines changed

9 files changed

+265
-79
lines changed

CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ set_target_properties(
7575
mmcc
7676
pointer-analysis
7777
solvers
78-
string_utils
7978
test-bigint
8079
testing-utils
8180
unit

src/util/string_utils.cpp

+21-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ Author: Daniel Poetzl
77
\*******************************************************************/
88

99
#include "string_utils.h"
10+
#include "invariant.h"
1011

1112
#include <cassert>
1213
#include <cctype>
1314
#include <algorithm>
1415

16+
/// Remove all whitespace characters from either end of a string. Whitespace
17+
/// in the middle of the string is left unchanged
18+
/// \param s: the string to strip
19+
/// \return The stripped string
1520
std::string strip_string(const std::string &s)
1621
{
1722
auto pred=[](char c){ return std::isspace(c); };
@@ -30,15 +35,26 @@ std::string strip_string(const std::string &s)
3035
return s.substr(i, (j-i+1));
3136
}
3237

38+
/// Given a string s, split into a sequence of substrings when separated by
39+
/// specified delimiter.
40+
/// \param s: The string to split up
41+
/// \param delim: The character to use as the delimiter
42+
/// \param [out] result: The sub strings. Must be empty.
43+
/// \param strip: If true, strip_string will be used on each element, removing
44+
/// whitespace from the beginning and end of each element
45+
/// \param remove_empty: If true, all empty-string elements will be removed.
46+
/// This is applied after strip so whitespace only elements will be removed if
47+
/// both are set to true
3348
void split_string(
3449
const std::string &s,
3550
char delim,
3651
std::vector<std::string> &result,
3752
bool strip,
3853
bool remove_empty)
3954
{
40-
assert(result.empty());
41-
assert(!std::isspace(delim));
55+
PRECONDITION(result.empty());
56+
// delim can't be a space character if using strip
57+
PRECONDITION(!std::isspace(delim) || !strip);
4258

4359
if(s.empty())
4460
{
@@ -47,7 +63,7 @@ void split_string(
4763
}
4864

4965
std::string::size_type n=s.length();
50-
assert(n>0);
66+
INVARIANT(n > 0, "Empty string case should already be handled");
5167

5268
std::string::size_type start=0;
5369
std::string::size_type i;
@@ -87,7 +103,8 @@ void split_string(
87103
std::string &right,
88104
bool strip)
89105
{
90-
assert(!std::isspace(delim));
106+
// delim can't be a space character if using strip
107+
PRECONDITION(!std::isspace(delim) || !strip);
91108

92109
std::vector<std::string> result;
93110

src/util/string_utils.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ std::string strip_string(const std::string &s);
1818

1919
void split_string(
2020
const std::string &s,
21-
char delim, // must not be a whitespace character
21+
char delim,
2222
std::vector<std::string> &result,
23-
bool strip=false, // strip whitespace from elements
24-
bool remove_empty=false); // remove empty elements
23+
bool strip = false,
24+
bool remove_empty = false);
2525

2626
void split_string(
2727
const std::string &s,

unit/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Unit test binaries
22
miniBDD
33
sharing_node
4-
string_utils
54
unit_tests

unit/CMakeLists.txt

-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ file(GLOB_RECURSE testing_utils "testing-utils/*.cpp" "testing-utils/*.h")
55
list(REMOVE_ITEM sources
66
# Used in executables
77
${CMAKE_CURRENT_SOURCE_DIR}/miniBDD.cpp
8-
${CMAKE_CURRENT_SOURCE_DIR}/string_utils.cpp
98

109
# Don't build
1110
${CMAKE_CURRENT_SOURCE_DIR}/sharing_map.cpp
@@ -63,14 +62,3 @@ target_include_directories(miniBDD
6362
target_link_libraries(miniBDD solvers ansi-c)
6463
add_test(NAME miniBDD COMMAND $<TARGET_FILE:miniBDD>)
6564
set_tests_properties(miniBDD PROPERTIES LABELS "CORE;CBMC")
66-
67-
add_executable(string_utils string_utils.cpp)
68-
target_include_directories(string_utils
69-
PUBLIC
70-
${CBMC_BINARY_DIR}
71-
${CBMC_SOURCE_DIR}
72-
${CMAKE_CURRENT_SOURCE_DIR}
73-
)
74-
target_link_libraries(string_utils solvers ansi-c)
75-
add_test(NAME string_utils COMMAND $<TARGET_FILE:string_utils>)
76-
set_tests_properties(string_utils PROPERTIES LABELS "CORE;CBMC")

unit/Makefile

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
# Source files for test utilities
44
SRC = unit_tests.cpp \
55
catch_example.cpp \
6-
util/expr_iterator.cpp \
7-
util/optional.cpp \
8-
analyses/call_graph.cpp \
9-
java_bytecode/java_bytecode_convert_class/convert_abstract_class.cpp \
106
# Empty last line
117

128
# Test source files
@@ -48,8 +44,11 @@ SRC += unit_tests.cpp \
4844
util/irep.cpp \
4945
util/irep_sharing.cpp \
5046
util/message.cpp \
47+
util/optional.cpp \
5148
util/parameter_indices.cpp \
5249
util/simplify_expr.cpp \
50+
util/string_utils/split_string.cpp \
51+
util/string_utils/strip_string.cpp \
5352
util/symbol_table.cpp \
5453
catch_example.cpp \
5554
java_bytecode/java_virtual_functions/virtual_functions.cpp \
@@ -88,7 +87,6 @@ OBJ += $(CPROVER_LIBS) testing-utils/testing-utils$(LIBEXT)
8887

8988
TESTS = unit_tests$(EXEEXT) \
9089
miniBDD$(EXEEXT) \
91-
string_utils$(EXEEXT) \
9290
# Empty last line
9391

9492
CLEANFILES = $(TESTS)
@@ -107,6 +105,3 @@ unit_tests$(EXEEXT): $(OBJ)
107105

108106
miniBDD$(EXEEXT): miniBDD$(OBJEXT) $(CPROVER_LIBS)
109107
$(LINKBIN)
110-
111-
string_utils$(EXEEXT): string_utils$(OBJEXT) $(CPROVER_LIBS)
112-
$(LINKBIN)

unit/string_utils.cpp

-50
This file was deleted.

0 commit comments

Comments
 (0)