[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] (no subject)
From: |
Greg Chicares |
Subject: |
[lmi-commits] (no subject) |
Date: |
Tue, 31 May 2016 21:06:22 +0000 (UTC) |
branch: master
commit cf7ed5f9bbe62c3482668346b080472552ab0de6
Author: Gregory W. Chicares <address@hidden>
Date: Tue May 31 14:57:54 2016 +0000
Make string begins_with(), ends_with() functions generally available
---
bcc_ld.cpp | 18 +-----------------
miscellany.cpp | 15 +++++++++++++++
miscellany.hpp | 8 ++++++++
miscellany_test.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
objects.make | 1 +
5 files changed, 74 insertions(+), 17 deletions(-)
diff --git a/bcc_ld.cpp b/bcc_ld.cpp
index 40d86da..c183f2f 100644
--- a/bcc_ld.cpp
+++ b/bcc_ld.cpp
@@ -38,6 +38,7 @@
#include "getopt.hpp"
#include "handle_exceptions.hpp"
#include "license.hpp"
+#include "miscellany.hpp" // ends_with()
#include "system_command.hpp"
#include <algorithm>
@@ -70,23 +71,6 @@ namespace
return t;
}
-#if 0 // Potentially useful, yet unused.
- bool begins_with(std::string const& s, std::string const& prefix)
- {
- return prefix == s.substr(0, prefix.size());
- }
-#endif // 0
-
- bool ends_with(std::string const& s, std::string const& suffix)
- {
- std::string::size_type pos = s.rfind(suffix);
- if(std::string::npos == pos)
- {
- return false;
- }
- return s.substr(pos).size() == suffix.size();
- }
-
// TRICKY COMPILER !! Contrary to the borland documentation,
// library paths *must* be given as
// /L$(bc_owl_path)/lib;$(bcXXXdir)/LIB
diff --git a/miscellany.cpp b/miscellany.cpp
index 25927df..97e3e18 100644
--- a/miscellany.cpp
+++ b/miscellany.cpp
@@ -126,6 +126,21 @@ std::string htmlize(std::string const& raw_text)
return html;
}
+bool begins_with(std::string const& s, std::string const& prefix)
+{
+ return prefix == s.substr(0, prefix.size());
+}
+
+bool ends_with(std::string const& s, std::string const& suffix)
+{
+ std::string::size_type pos = s.rfind(suffix);
+ if(std::string::npos == pos)
+ {
+ return false;
+ }
+ return s.substr(pos).size() == suffix.size();
+}
+
void ltrim(std::string& s, char const* superfluous)
{
std::string::size_type p = s.find_first_not_of(superfluous);
diff --git a/miscellany.hpp b/miscellany.hpp
index 3f49763..627373e 100644
--- a/miscellany.hpp
+++ b/miscellany.hpp
@@ -96,6 +96,14 @@ std::vector<std::string> LMI_SO split_into_lines(std::string
const&);
std::string htmlize(std::string const&);
+/// Ascertain whether string begins with prefix.
+
+bool LMI_SO begins_with(std::string const& s, std::string const& prefix);
+
+/// Ascertain whether string begins ends with suffix.
+
+bool LMI_SO ends_with(std::string const& s, std::string const& suffix);
+
/// Remove superfluous characters from beginning of string.
void LMI_SO ltrim(std::string& s, char const* superfluous);
diff --git a/miscellany_test.cpp b/miscellany_test.cpp
index 3750590..fb4e3e1 100644
--- a/miscellany_test.cpp
+++ b/miscellany_test.cpp
@@ -97,6 +97,54 @@ void test_minmax()
#endif // !defined __BORLANDC__
}
+void test_prefix_and_suffix()
+{
+ std::string s = "";
+
+ BOOST_TEST( begins_with(s, ""));
+ BOOST_TEST( ends_with (s, ""));
+
+ BOOST_TEST(!begins_with(s, "A"));
+ BOOST_TEST(!ends_with (s, "Z"));
+
+ BOOST_TEST(!begins_with(s, "ABC"));
+ BOOST_TEST(!ends_with (s, "XYZ"));
+
+ s = "W";
+
+ BOOST_TEST( begins_with(s, ""));
+ BOOST_TEST( ends_with (s, ""));
+
+ BOOST_TEST(!begins_with(s, "A"));
+ BOOST_TEST(!ends_with (s, "Z"));
+
+ BOOST_TEST(!begins_with(s, "WW"));
+ BOOST_TEST(!ends_with (s, "WW"));
+
+ BOOST_TEST( begins_with(s, "W"));
+ BOOST_TEST( ends_with (s, "W"));
+
+ s = "LMNOP";
+
+ BOOST_TEST( begins_with(s, ""));
+ BOOST_TEST( ends_with (s, ""));
+
+ BOOST_TEST(!begins_with(s, "A"));
+ BOOST_TEST(!ends_with (s, "Z"));
+
+ BOOST_TEST( begins_with(s, "L"));
+ BOOST_TEST( ends_with (s, "P"));
+
+ BOOST_TEST( begins_with(s, "LMN"));
+ BOOST_TEST( ends_with (s, "NOP"));
+
+ BOOST_TEST( begins_with(s, "LMNOP"));
+ BOOST_TEST( ends_with (s, "LMNOP"));
+
+ BOOST_TEST(!begins_with(s, "LMNOPQ"));
+ BOOST_TEST(!ends_with (s, "KLMNOP"));
+}
+
void test_trimming()
{
char const*const superfluous = " ;";
@@ -134,6 +182,7 @@ int test_main(int, char*[])
{
test_files_are_identical();
test_minmax();
+ test_prefix_and_suffix();
test_trimming();
return 0;
diff --git a/objects.make b/objects.make
index 6040ab5..bfb98a7 100644
--- a/objects.make
+++ b/objects.make
@@ -1006,6 +1006,7 @@ bcc_ld$(EXEEXT): \
bcc_ld.o \
getopt.o \
license.o \
+ miscellany.o \
system_command.o \
system_command_non_wx.o \
- [lmi-commits] [lmi] master updated (cdd9aa4 -> 37c5a98), Greg Chicares, 2016/05/31
- [lmi-commits] (no subject), Greg Chicares, 2016/05/31
- [lmi-commits] (no subject), Greg Chicares, 2016/05/31
- [lmi-commits] (no subject), Greg Chicares, 2016/05/31
- [lmi-commits] (no subject), Greg Chicares, 2016/05/31
- [lmi-commits] (no subject),
Greg Chicares <=
- [lmi-commits] (no subject), Greg Chicares, 2016/05/31