From 44ef56941e945eaf8f06d3cacc5ed1b82dd56309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 23 Oct 2016 15:28:28 +0200 Subject: [PATCH] made an index_of() method for StringValue, which calculates the index of a char --- StringValue.cpp | 11 ++++++++++- StringValue.h | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/StringValue.cpp b/StringValue.cpp index 90426ad..cee8e78 100644 --- a/StringValue.cpp +++ b/StringValue.cpp @@ -37,4 +37,13 @@ StringValue::operator const char*() const { return _data; } size_t StringValue::size() const -{ return _size - 1; } \ No newline at end of file +{ return _size - 1; } + +size_t StringValue::index_of(char* c) const +{ + size_t index = c - _data; // this is totally safe pointer-arithmetic + if (index <= this->size()) // if it is within our data + return index; + else + throw std::invalid_argument("char* not within held data!"); +} \ No newline at end of file diff --git a/StringValue.h b/StringValue.h index 1a65bba..5cebb8a 100644 --- a/StringValue.h +++ b/StringValue.h @@ -28,4 +28,5 @@ public: char& operator[](size_t index); operator const char*() const; size_t size() const; + size_t index_of(char* c) const; }; \ No newline at end of file