From d9c377b51c653e64f3d66b85494489cd159e55ea Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Mon, 7 Mar 2016 11:02:58 -0500 Subject: [PATCH] lib/core/text: add a last_separator to `Collection::join` Signed-off-by: Jean Privat --- lib/core/text/abstract_text.nit | 16 +++++++++++++--- lib/pthreads/concurrent_collections.nit | 4 ++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/core/text/abstract_text.nit b/lib/core/text/abstract_text.nit index 0d2848c..405eb76 100644 --- a/lib/core/text/abstract_text.nit +++ b/lib/core/text/abstract_text.nit @@ -1887,7 +1887,11 @@ redef class Collection[E] # assert [1, 2, 3].join(":") == "1:2:3" # assert [1..3].join(":") == "1:2:3" # assert [1..3].join == "123" - fun join(separator: nullable Text): String + # + # if `last_separator` is given, then it is used to separate the last element. + # + # assert [1, 2, 3, 4].join(", ", " and ") == "1, 2, 3 and 4" + fun join(separator: nullable Text, last_separator: nullable Text): String do if is_empty then return "" @@ -1898,13 +1902,19 @@ redef class Collection[E] var e = i.item if e != null then s.append(e.to_s) + if last_separator == null then last_separator = separator + # Concat other items i.next while i.is_ok do - if separator != null then s.append(separator) e = i.item - if e != null then s.append(e.to_s) i.next + if i.is_ok then + if separator != null then s.append(separator) + else + if last_separator != null then s.append(last_separator) + end + if e != null then s.append(e.to_s) end return s.to_s end diff --git a/lib/pthreads/concurrent_collections.nit b/lib/pthreads/concurrent_collections.nit index d55bad0..e0c53af 100644 --- a/lib/pthreads/concurrent_collections.nit +++ b/lib/pthreads/concurrent_collections.nit @@ -161,10 +161,10 @@ abstract class ConcurrentCollection[E] return r end - redef fun join(sep) + redef fun join(sep, last_sep) do mutex.lock - var r = real_collection.join(sep) + var r = real_collection.join(sep, last_sep) mutex.unlock return r end -- 1.7.9.5