Skip to content

Commit 7316651

Browse files
committed
Update Repeat and Multiply accordingly.
Signed-off-by: Mei Chu <meimchu@gmail.com>
1 parent 95422c1 commit 7316651

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/utils/StringUtils.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ inline std::string Multiply(const std::string & str, size_t n)
347347
if (n == 0) { return ""; }
348348
if (n == 1) { return str; }
349349

350+
if (str.empty()) { return ""; }
351+
350352
std::ostringstream os;
351353
for(size_t i = 0; i < n; ++i) { os << str; }
352354
return os.str();
@@ -358,12 +360,9 @@ inline std::string Repeat(const std::string & str, size_t n)
358360
// Early exit and match pystring::mul behaviour.
359361
if (n == 0) { return {}; }
360362
if (n == 1) { return str; }
361-
const auto str_size = str.size();
362363

363-
// Check for overflow if n is greater than maximum allowable repeat with string max_size.
364-
// New behaviour compared to pystring::mul and StringUtil::Multiply.
365-
// limits.h says size_t max size is 18446744073709551615.
366-
if (n > str.max_size() / str_size) { return {}; }
364+
if (str.empty()) { return {}; }
365+
const auto str_size = str.size();
367366

368367
std::string result;
369368
result.reserve(str_size * n);

tests/cpu/ops/noop/NoOps_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ OCIO_ADD_TEST(NoOps, partition_gpu_ops)
242242
OCIO_CHECK_NO_THROW( AssertPartitionIntegrity(gpuPreOps,
243243
gpuLatticeOps,
244244
gpuPostOps) );
245+
std::cerr << "gpuPreOps" << std::endl;
246+
std::cerr << SerializeOpVec(gpuPreOps, 4) << std::endl;
245247
/*
246248
std::cerr << "gpuPreOps" << std::endl;
247249
std::cerr << SerializeOpVec(gpuPreOps, 4) << std::endl;

tests/utils/StringUtils_tests.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ OCIO_ADD_TEST(StringUtils, split)
131131

132132
// Something important to notice and preserve.
133133
{
134-
// Note: StringUtils::Split() is mainly used to parse some string content enumerating
134+
// Note: StringUtils::Split() is mainly used to parse some string content enumerating
135135
// a list of substrings (i.e. separator could be a space, comma, etc). In that use case,
136136
// a string like ",," must return three entries. Refer to 'looks' parsing for example.
137137
// However, StringUtils::SplitByLines() is mainly used to read some file content where
@@ -287,6 +287,10 @@ OCIO_ADD_TEST(StringUtils, multiply)
287287
OCIO_CHECK_EQUAL(StringUtils::Multiply(" ", 0), "");
288288
OCIO_CHECK_EQUAL(StringUtils::Multiply(" ", 1), " ");
289289
OCIO_CHECK_EQUAL(StringUtils::Multiply(" ", 2), " ");
290+
291+
OCIO_CHECK_EQUAL(StringUtils::Multiply("", 0), "");
292+
OCIO_CHECK_EQUAL(StringUtils::Multiply("", 1), "");
293+
OCIO_CHECK_EQUAL(StringUtils::Multiply("", 2), "");
290294
}
291295

292296
OCIO_ADD_TEST(StringUtils, repeat)
@@ -300,4 +304,8 @@ OCIO_ADD_TEST(StringUtils, repeat)
300304
OCIO_CHECK_EQUAL(StringUtils::Repeat(" ", 0), "");
301305
OCIO_CHECK_EQUAL(StringUtils::Repeat(" ", 1), " ");
302306
OCIO_CHECK_EQUAL(StringUtils::Repeat(" ", 2), " ");
307+
308+
OCIO_CHECK_EQUAL(StringUtils::Repeat("", 0), "");
309+
OCIO_CHECK_EQUAL(StringUtils::Repeat("", 1), "");
310+
OCIO_CHECK_EQUAL(StringUtils::Repeat("", 2), "");
303311
}

0 commit comments

Comments
 (0)