Element InterpretSuperString_(SuperString str) { Element out = Nil(); std::string const INPUT(str.Value()); if (false == INPUT.empty()) { StringHolder temp = str.GetStringHolder(); out = SuperString(StringHolder(temp, 1, INPUT.size() - 1)); } return out; }
Element Interpret_( Environment& , std::vector<Element> const& parms , Element const&) { if (parms.size() != 3) { std::stringstream ss; ss << "subseq requires 3 arguments, a container/string and start/end values"; return Error(ss.str()); } bool gotANumber1 = false; bool gotANumber2 = false; bool gotAContainer = false; bool gotAString = false; bool gotASuperString = false; Number N1 = CastToNumber(parms[1], gotANumber1); Number N2 = CastToNumber(parms[2], gotANumber2); Container C = CastToContainer(parms[0], gotAContainer); String S = CastToString(parms[0], gotAString); SuperString SS = CastToSuperString(parms[0], gotASuperString); if (gotANumber1 && gotANumber2 && (gotAContainer || gotAString || gotASuperString)) { int index1 = N1.IntValue(); int index2 = N2.IntValue(); if (index1 < 0 || index2 < 0) { std::stringstream ss; ss << "subseq requires a positive range, received: " << index1 << " and " << index2; return Error(ss.str()); } if (gotAContainer) { if (C.NumberOfElements() <= index1 && C.NumberOfElements() <= index2) { return Container(); } std::vector<Element> elements; C.RetrieveVector(elements); elements.assign(elements.begin() + N1.IntValue(), elements.begin() + N2.IntValue()); Container out; out.SetVector(elements); return out; } if (gotAString) { std::string value = S.Value(); if (index1 >= static_cast<int>(value.size())) { return String(""); } value = value.substr(N1.IntValue(), N2.IntValue()); return String(value); } if (gotASuperString) { StringHolder temp = SS.GetStringHolder(); return SuperString(StringHolder(temp, N1.IntValue(), N2.IntValue())); } return Error("Something unexpected happened in subseq"); } else { std::stringstream ss; ss << "subseq requires 3 arguments, a container/string and start/end numeric values.\n" << "Received: " << parms[0].ToString() << ", " << parms[1].ToString() << ", " << parms[2].ToString(); return Error(ss.str()); } }