Skip to content

Commit

Permalink
Adds explode.
Browse files Browse the repository at this point in the history
  • Loading branch information
rioki committed May 22, 2012
1 parent 3588fc8 commit ef41a7a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
13 changes: 7 additions & 6 deletions Makefile
Expand Up @@ -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
Expand All @@ -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

Expand Down
12 changes: 12 additions & 0 deletions include/strex/strings.h
@@ -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
40 changes: 40 additions & 0 deletions src/strings.cpp
@@ -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;
}

}

40 changes: 40 additions & 0 deletions test/test_strings.cpp
@@ -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.