From 8285d4285d414701dc57e66a35316a5979536611 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Christophe=20Beaupr=C3=A9?= Date: Sun, 7 Dec 2014 11:30:50 -0500 Subject: [PATCH] ropes: Fix the behavior of `RopeBuffer.clear` when used with `to_s`. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jean-Christophe Beaupré --- lib/standard/ropes.nit | 4 +++ tests/sav/test_ropes_buffer_to_s.res | 4 +++ tests/test_ropes_buffer_to_s.nit | 58 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 tests/sav/test_ropes_buffer_to_s.res create mode 100644 tests/test_ropes_buffer_to_s.nit diff --git a/lib/standard/ropes.nit b/lib/standard/ropes.nit index df98433..8d2d057 100644 --- a/lib/standard/ropes.nit +++ b/lib/standard/ropes.nit @@ -241,6 +241,10 @@ class RopeBuffer length = 0 rpos = 0 dumped = 0 + if written then + ns = new NativeString(buf_size) + written = false + end end redef fun substring(from, count) do diff --git a/tests/sav/test_ropes_buffer_to_s.res b/tests/sav/test_ropes_buffer_to_s.res new file mode 100644 index 0000000..0bf725e --- /dev/null +++ b/tests/sav/test_ropes_buffer_to_s.res @@ -0,0 +1,4 @@ +`clear` and `append`: abcd +`clear` and `add`: ab +`add` at `maxlen + 1`: c +`append` up to `maxlen + 1`: ab diff --git a/tests/test_ropes_buffer_to_s.nit b/tests/test_ropes_buffer_to_s.nit new file mode 100644 index 0000000..4933a1b --- /dev/null +++ b/tests/test_ropes_buffer_to_s.nit @@ -0,0 +1,58 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Checks the immutability of the strings returned by `RopeBuffer.to_s`. +module test_ropes_buffer_to_s + +import standard + +# Note: In this sort of test, never print the string more than once: the string +# itself may cache an flatten representation of itself when `print` calls `to_s` +# on it. For example, the original bug that motivated the writting of the +# present test got unoticed until we tried to edit the buffer **before** +# outputting `s`. + +var buffer = new RopeBuffer +var s: String + +sys.stdout.write "`clear` and `append`: " +buffer.append "abcd" +s = buffer.to_s +buffer.clear +buffer.append "ef" +print s + +sys.stdout.write "`clear` and `add`: " +buffer.clear +buffer.add 'a' +buffer.add 'b' +s = buffer.to_s +buffer.clear +buffer.add 'c' +print s + +sys.stdout.write "`add` at `maxlen + 1`: " +buffer.clear +buffer.add 'c' +s = buffer.to_s +buffer.append("*" * (maxlen -1)) +buffer.add 'x' +print s + +sys.stdout.write "`append` up to `maxlen + 1`: " +buffer.clear +buffer.append "ab" +s = buffer.to_s +buffer.append("*" * (maxlen -1)) +print s -- 1.7.9.5