From 14b9bd8c9aa8999674727848afbf66e28359acf2 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bulski Date: Mon, 9 Apr 2018 18:24:27 +0200 Subject: [PATCH] shortcuts, return full string instead of copying it --- kaitai/kaitaistream.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/kaitai/kaitaistream.cpp b/kaitai/kaitaistream.cpp index 97542e8..81d0d48 100644 --- a/kaitai/kaitaistream.cpp +++ b/kaitai/kaitaistream.cpp @@ -399,11 +399,15 @@ std::string kaitai::kstream::ensure_fixed_contents(std::string expected) { } std::string kaitai::kstream::bytes_strip_right(std::string src, char pad_byte) { - std::size_t new_len = src.length(); + std::size_t max_len = src.length(); + std::size_t new_len = max_len; while (new_len > 0 && src[new_len - 1] == pad_byte) new_len--; + if (new_len == max_len) + return src; + return src.substr(0, new_len); } @@ -417,6 +421,9 @@ std::string kaitai::kstream::bytes_terminate(std::string src, char term, bool in if (include && new_len < max_len) new_len++; + if (new_len == max_len) + return src; + return src.substr(0, new_len); }