Skip to content

Commit 3f25a77

Browse files
committed
#209-test slices
Completed. Validated. A few fixes and added `operator*()`.
1 parent 43f4bbd commit 3f25a77

File tree

1 file changed

+62
-36
lines changed

1 file changed

+62
-36
lines changed

cpp-strings/cppstrings.h

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,9 +1846,7 @@ namespace pcs // i.e. "pythonic c++ strings"
18461846
static constexpr IntT DEFAULT{ std::numeric_limits<IntT>::min()};
18471847

18481848
//--- Constructors / Destructor -------------------
1849-
Slice() noexcept = default; //!< Default constructor
1850-
1851-
Slice(const IntT start, const IntT stop, const IntT step) noexcept //!< Valued constructor
1849+
Slice(const IntT start = DEFAULT, const IntT stop = DEFAULT, const IntT step = DEFAULT) noexcept //!< Valued constructor
18521850
: _start(start)
18531851
, _stop(stop)
18541852
, _step(step)
@@ -1873,15 +1871,21 @@ namespace pcs // i.e. "pythonic c++ strings"
18731871
return _step == 0 ? true : _step > 0 ? _index >= _stop : _index <= _stop;
18741872
}
18751873

1876-
inline const IntT operator++() noexcept //!< iterates one step, pre-increment. Caution: returned index may be out of bounds. Check '!end()' before using its value.
1874+
inline Slice operator++() noexcept //!< iterates one step, pre-increment. Caution: index may be out of bounds. Check '!end()' before dereferencing the slice.
18771875
{
1878-
return _index += _step;
1876+
_index += _step;
1877+
return *this;
18791878
}
18801879

1881-
inline const IntT operator++(int) noexcept //!< iterates one step, post-increment. Caution: returned index may be out of bounds. Check '!end()' before using its value.
1880+
inline Slice operator++(int) noexcept //!< iterates one step, post-increment. Caution: index may be out of bounds. Check '!end()' before dereferencing the slice.
18821881
{
18831882
_index += _step;
1884-
return _index - _step;
1883+
return *this;
1884+
}
1885+
1886+
inline const IntT operator*() noexcept //!< dereferences the slice.
1887+
{
1888+
return _index;
18851889
}
18861890

18871891

@@ -1896,16 +1900,38 @@ namespace pcs // i.e. "pythonic c++ strings"
18961900
{
18971901
if (_start == DEFAULT)
18981902
_start = 0;
1899-
else if (_start < 0)
1903+
else if (_start < 0) {
19001904
_start += str_size;
1901-
1902-
if (_stop == DEFAULT)
1903-
_stop = str_size;
1904-
else if (_stop < 0)
1905+
if (_start < 0)
1906+
_start = 0;
1907+
}
1908+
else if (_start >= str_size)
1909+
_start = str_size - 1;
1910+
1911+
if (_stop == DEFAULT) {
1912+
if (_step < 0 && _step != DEFAULT)
1913+
_stop = 0;
1914+
else
1915+
_stop = str_size;
1916+
}
1917+
else if (_stop < 0) {
19051918
_stop += str_size;
1919+
if (_stop < 0)
1920+
_stop = 0;
1921+
}
1922+
else if (_stop > str_size)
1923+
_stop = str_size;
19061924

19071925
if (_step == DEFAULT)
19081926
_step = 1;
1927+
if (_step < 0) {
1928+
if (_start <= _stop)
1929+
_step = 0; // will force end() to true
1930+
}
1931+
else {
1932+
if (_start >= _stop)
1933+
_step = 0; // will force end() to true
1934+
}
19091935

19101936
return _index = _start;
19111937
}
@@ -1917,11 +1943,11 @@ namespace pcs // i.e. "pythonic c++ strings"
19171943
requires std::is_signed_v<IntT>
19181944
struct StartSlice : public Slice<IntT>
19191945
{
1920-
//--- Constructors / Destructor -------------------
1921-
StartSlice() noexcept = default; //!< Default constructor
1946+
using MyBaseClass = Slice<IntT>;
19221947

1923-
inline StartSlice(const IntT start) noexcept //!< Valued constructor
1924-
: Slice(start, Slice::DEFAULT, 1)
1948+
//--- Constructors / Destructor -------------------
1949+
inline StartSlice(const IntT start = MyBaseClass::DEFAULT) noexcept //!< Valued constructor
1950+
: MyBaseClass(start, MyBaseClass::DEFAULT, 1)
19251951
{}
19261952

19271953
virtual ~StartSlice() noexcept = default; //!< Default destructor.
@@ -1933,11 +1959,11 @@ namespace pcs // i.e. "pythonic c++ strings"
19331959
requires std::is_signed_v<IntT>
19341960
struct StopSlice : public Slice<IntT>
19351961
{
1936-
//--- Constructors / Destructor -------------------
1937-
StopSlice() noexcept = default; //!< Default constructor
1962+
using MyBaseClass = Slice<IntT>;
19381963

1939-
inline StopSlice(const IntT stop) noexcept //!< Valued constructor
1940-
: Slice(Slice::DEFAULT, stop, 1)
1964+
//--- Constructors / Destructor -------------------
1965+
inline StopSlice(const IntT stop = MyBaseClass::DEFAULT) noexcept //!< Valued constructor
1966+
: MyBaseClass(MyBaseClass::DEFAULT, stop, 1)
19411967
{}
19421968

19431969
virtual ~StopSlice() noexcept = default; //!< Default destructor.
@@ -1949,11 +1975,11 @@ namespace pcs // i.e. "pythonic c++ strings"
19491975
requires std::is_signed_v<IntT>
19501976
struct StepSlice : public Slice<IntT>
19511977
{
1952-
//--- Constructors / Destructor -------------------
1953-
StepSlice() noexcept = default; //!< Default constructor
1978+
using MyBaseClass = Slice<IntT>;
19541979

1955-
inline StepSlice(const IntT step) noexcept //!< Valued constructor
1956-
: Slice(Slice::DEFAULT, Slice::DEFAULT, step)
1980+
//--- Constructors / Destructor -------------------
1981+
inline StepSlice(const IntT step = MyBaseClass::DEFAULT) noexcept //!< Valued constructor
1982+
: MyBaseClass(MyBaseClass::DEFAULT, MyBaseClass::DEFAULT, step)
19571983
{}
19581984

19591985
virtual ~StepSlice() noexcept = default; //!< Default destructor.
@@ -1965,11 +1991,11 @@ namespace pcs // i.e. "pythonic c++ strings"
19651991
requires std::is_signed_v<IntT>
19661992
struct StartStopSlice : public Slice<IntT>
19671993
{
1968-
//--- Constructors / Destructor -------------------
1969-
StartStopSlice() noexcept = default; //!< Default constructor
1994+
using MyBaseClass = Slice<IntT>;
19701995

1971-
inline StartStopSlice(const IntT start, const IntT stop) noexcept //!< Valued constructor
1972-
: Slice(start, stop, 1)
1996+
//--- Constructors / Destructor -------------------
1997+
inline StartStopSlice(const IntT start = MyBaseClass::DEFAULT, const IntT stop = MyBaseClass::DEFAULT) noexcept //!< Valued constructor
1998+
: MyBaseClass(start, stop, 1)
19731999
{}
19742000

19752001
virtual ~StartStopSlice() noexcept = default; //!< Default destructor.
@@ -1981,11 +2007,11 @@ namespace pcs // i.e. "pythonic c++ strings"
19812007
requires std::is_signed_v<IntT>
19822008
struct StartStepSlice : public Slice<IntT>
19832009
{
1984-
//--- Constructors / Destructor -------------------
1985-
StartStepSlice() noexcept = default; //!< Default constructor
2010+
using MyBaseClass = Slice<IntT>;
19862011

1987-
inline StartStepSlice(const IntT start, const IntT step) noexcept //!< Valued constructor
1988-
: Slice(start, Slice::DEFAULT, step)
2012+
//--- Constructors / Destructor -------------------
2013+
inline StartStepSlice(const IntT start = MyBaseClass::DEFAULT, const IntT step = MyBaseClass::DEFAULT) noexcept //!< Valued constructor
2014+
: MyBaseClass(start, MyBaseClass::DEFAULT, step)
19892015
{}
19902016

19912017
virtual ~StartStepSlice() noexcept = default; //!< Default destructor.
@@ -1998,11 +2024,11 @@ namespace pcs // i.e. "pythonic c++ strings"
19982024
requires std::is_signed_v<IntT>
19992025
struct StopStepSlice : public Slice<IntT>
20002026
{
2001-
//--- Constructors / Destructor -------------------
2002-
StopStepSlice() noexcept = default; //!< Default constructor
2027+
using MyBaseClass = Slice<IntT>;
20032028

2004-
inline StopStepSlice(const IntT stop, const IntT step) noexcept //!< Valued constructor
2005-
: Slice(Slice::DEFAULT, stop, step)
2029+
//--- Constructors / Destructor -------------------
2030+
inline StopStepSlice(const IntT stop = MyBaseClass::DEFAULT, const IntT step = MyBaseClass::DEFAULT) noexcept //!< Valued constructor
2031+
: MyBaseClass(MyBaseClass::DEFAULT, stop, step)
20062032
{}
20072033

20082034
virtual ~StopStepSlice() noexcept = default; //!< Default destructor.

0 commit comments

Comments
 (0)