Commit c59846ef authored by Alexander A. Maly's avatar Alexander A. Maly

Trying to optimize string indexing

parent 1ad918af
Pipeline #3039 passed with stages
in 3 minutes and 57 seconds
......@@ -77,7 +77,25 @@ void Variable::setValue(const AnyValue &v)
}
const String &Variable::toStringRef() const
{
static const String empty;
if (reference_) {
if (referenceIndeces_[3] != 0) {
Kumir::Core::abort(Kumir::Core::fromUtf8("Плохая ссылка на строку"));
return empty;
}
return reference_->toStringRef();
}
if (baseType_ != VT_string) {
Kumir::Core::abort(Kumir::Core::fromUtf8("Величина не строковая"));
return empty;
}
return value_.toStringRef();
}
String Variable::toString() const
......
......@@ -265,6 +265,16 @@ public:
}
}
const String &toStringRef() const
{
static const String empty;
if (svalue_ == 0) {
Kumir::Core::abort(Kumir::Core::fromUtf8("Значение не строковое"));
return empty;
}
return *svalue_;
}
String toString() const
{
if (type_ == VT_int) {
......@@ -701,6 +711,7 @@ public:
return value().toChar();
}
const String &toStringRef() const;
String toString() const;
String toString(int indeces[4]) const;
......
......@@ -1272,9 +1272,10 @@ void KumirVM::do_stdcall(uint16_t alg)
}
/* алг цел длин(лит s) */
case 0x001f: {
const String x = valuesStack_.pop().toString();
int y = x.length();
valuesStack_.push(Variable(y));
const String &s = valuesStack_.top().toStringRef();
size_t l = s.length();
valuesStack_.pop();
valuesStack_.push(Variable((int) l));
break;
}
/* алг цел код(сим ch) */
......@@ -1889,13 +1890,17 @@ void KumirVM::do_specialcall(uint16_t alg)
if (stacksMutex_) {
stacksMutex_->lock();
}
Variable second = valuesStack_.pop();
Variable first = valuesStack_.pop();
int index = second.value().toInt();
const String &s = first.value().toString();
Variable first = valuesStack_.pop();
//const String &s = first.value().toString();
const String &s = first.toStringRef();
error_ = Kumir::Core::getError();
if (error_.length() == 0) {
if (index < 1 || index > (int)s.length()) {
if (index < 1 || index > (int) s.length()) {
error_ = Kumir::Core::fromUtf8("Индекс символа больше длины строки");
} else {
Char result = s[index - 1];
......@@ -1903,6 +1908,7 @@ void KumirVM::do_specialcall(uint16_t alg)
valuesStack_.push(r);
}
}
if (stacksMutex_) {
stacksMutex_->unlock();
}
......@@ -1942,7 +1948,7 @@ void KumirVM::do_specialcall(uint16_t alg)
Variable first = valuesStack_.pop();
int start = second.value().toInt();
int end = third.value().toInt();
const String &s = first.value().toString();
const String &s = first.toStringRef();
error_ = Kumir::Core::getError();
if (error_.length() == 0) {
if (start < 1 || start > (int)s.length()) {
......@@ -1950,7 +1956,7 @@ void KumirVM::do_specialcall(uint16_t alg)
} else if (end < start) {
String empty;
valuesStack_.push(Variable(empty));
} else if (end < 1 || end > (int)s.length()) {
} else if (end < 1 || end > (int) s.length()) {
error_ = Kumir::Core::fromUtf8("Правая граница вырезки за пределами строки");
} else {
String result = s.substr(start - 1, end - start + 1);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment