Permalink
Browse files

Adds explode.

  • Loading branch information...
1 parent 3588fc8 commit ef41a7a88832dc53ee2c1793ef9aed327add61ea @rioki committed May 22, 2012
Showing with 99 additions and 6 deletions.
  1. +7 −6 Makefile
  2. +12 −0 include/strex/strings.h
  3. +40 −0 src/strings.cpp
  4. +40 −0 test/test_strings.cpp
View
@@ -6,13 +6,14 @@ CXXFLAGS += -Iinclude -Iinclude/strex
.PHONY: clean test
.SUFFIXES: .o .cpp
-all: lib/libstrex.a bin/strextest
+all: lib/libstrex.a
src = src/files.cpp \
src/strings.cpp
test_src = test/main.cpp \
test/test_compose.cpp \
- test_lexical_cast.cpp
+ test/test_lexical_cast.cpp \
+ test/test_strings.cpp
ifeq ($(MSYSTEM), MINGW32)
EXEEXT=*.exe
@@ -23,18 +24,18 @@ test_objs = $(patsubst %.cpp, %.o, $(test_src))
deps = $(subst .o,.d,$(objs))
test_deps = $(subst .o,.d,$(test_objs))
-lib/libstrex.a: src/files.o src/strings.o
+lib/libstrex.a: $(objs)
@echo Creating $@ library...
@$(AR) crf $@ $^
-bin/strextest: test/main.o test/test_compose.o test/test_lexical_cast.o lib/libstrex.a
+bin/strextest: $(test_objs) lib/libstrex.a
@echo Linking $@
@$(CXX) $(CXXFLAGS) $(LDFLAGS) $^ -lUnitTest++ -Llib -lstrex -o $@
clean:
- rm -f src/*.o src/*.d test/*.o test/*.d lib/libstrex.a bin/strextest$(EXEEXT)
+ rm -f */*.o */*.d lib/libstrex.a bin/strextest$(EXEEXT)
-test: bin/strextest
+test: bin/strextest
@echo Running unit tests.
@./bin/strextest
View
@@ -1 +1,13 @@
+#ifndef _STREX_STRINGS_H_
+#define _STREX_STRINGS_H_
+
+#include <string>
+#include <vector>
+
+namespace strex
+{
+ std::vector<std::string> explode(const std::string& str, const std::string& delim);
+}
+
+#endif
View
@@ -1 +1,41 @@
+#include "strings.h"
+
+namespace strex
+{
+//------------------------------------------------------------------------------
+ std::vector<std::string> explode(const std::string& str, const std::string& delim)
+ {
+ std::vector<std::string> gibs;
+ size_t start = 0;
+ size_t end = 0;
+
+ while ((start != std::string::npos) && (start < str.size()))
+ {
+ end = str.find(delim, start);
+
+ std::string gib;
+ if (end == std::string::npos)
+ {
+ gib = str.substr(start);
+ start = std::string::npos;
+ }
+ else
+ {
+ gib = str.substr(start, end - start);
+ start = end + delim.size();
+ }
+ gibs.push_back(gib);
+ }
+
+ // special case, when the delimiter is at the end
+ if (start == str.size())
+ {
+ gibs.push_back(std::string());
+ }
+
+ return gibs;
+ }
+
+}
+
View
@@ -0,0 +1,40 @@
+
+#include <UnitTest++/UnitTest++.h>
+
+#include <strex/strings.h>
+
+SUITE(lexcial_cast)
+{
+ TEST(explode)
+ {
+ std::string pattern = "one;two;three";
+ std::vector<std::string> bits = strex::explode(pattern, ";");
+
+ CHECK_EQUAL(3, bits.size());
+ CHECK_EQUAL("one", bits.at(0));
+ CHECK_EQUAL("two", bits.at(1));
+ CHECK_EQUAL("three", bits.at(2));
+ }
+
+ TEST(explode2)
+ {
+ std::string pattern = ";two;three";
+ std::vector<std::string> bits = strex::explode(pattern, ";");
+
+ CHECK_EQUAL(3, bits.size());
+ CHECK_EQUAL("", bits.at(0));
+ CHECK_EQUAL("two", bits.at(1));
+ CHECK_EQUAL("three", bits.at(2));
+ }
+
+ TEST(explode3)
+ {
+ std::string pattern = "one;two;";
+ std::vector<std::string> bits = strex::explode(pattern, ";");
+
+ CHECK_EQUAL(3, bits.size());
+ CHECK_EQUAL("one", bits.at(0));
+ CHECK_EQUAL("two", bits.at(1));
+ CHECK_EQUAL("", bits.at(2));
+ }
+}

0 comments on commit ef41a7a

Please sign in to comment.