Skip to content

Commit e1997ce

Browse files
committed
fix string multiplication
1 parent 828e780 commit e1997ce

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/value.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,19 @@ namespace rift {
113113

114114
// If either value is a string, repeat it n times, where n is the second value.
115115
if (isString() || other.isString()) {
116+
// make sure only one of them is a string
117+
if (isString() && other.isString()) {
118+
return string("<error: multiplication of strings>");
119+
}
120+
121+
// figure out which one is string
122+
const Value &str = isString() ? *this : other;
123+
const Value &num = isString() ? other : *this;
124+
125+
// repeat the string n times
116126
std::string result;
117-
auto count = other.isInteger() ? other.toInteger() : static_cast<int>(other.toFloat());
118-
for (int i = 0; i < count; ++i) {
119-
result += isString() ? getString() : other.getString();
127+
for (int i = 0; i < num.toInteger(); ++i) {
128+
result += str.toString();
120129
}
121130
return string(std::move(result));
122131
}

test/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ int main() {
9494
RIFT_TEST_CASE("{$'string interpolation: {2 + 2}'}", "string interpolation: 4");
9595
RIFT_TEST_CASE("{float('3.14')} {int('100')} {str(3.1415926)} {int('ABC')}", "3.14 100 3.14 NaN");
9696
RIFT_TEST_CASE("{precision(float('3.149268') * 1.5, 6)}", "4.723902");
97+
RIFT_TEST_CASE("{'*' * 5} {4 * '*'} {'*' * 3.0} {2.0 * '*'}", "***** **** *** **");
9798
}
9899

99100
// Show the results

0 commit comments

Comments
 (0)