From b1611d67fe6dab4f33920907112ad73fdd96a455 Mon Sep 17 00:00:00 2001
From: Tomas Stupka <tomas.stupka@oracle.com>
Date: Wed, 14 Feb 2018 18:07:08 +0100
Subject: [PATCH] fixed wrong prefix/suffix in string sequence

---
 .../oracle/truffle/r/nodes/builtin/base/Paste.java   | 12 +++++++++---
 .../truffle/r/test/builtins/TestBuiltin_paste.java   |  8 +++++++-
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Paste.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Paste.java
index 22e0871220..ce22e93b45 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Paste.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Paste.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -312,8 +312,14 @@ public abstract class Paste extends RBuiltinNode.Arg3 {
         }
         RIntSequence seq = (RIntSequence) values.getDataAt(seqPos);
         StringBuilder suffix = new StringBuilder();
-        for (int i = seqPos + 1; i < length; i++) {
-            suffix.append(values.getDataAt(i)).append(sep);
+        if (seqPos + 1 < length) {
+            suffix.append(sep);
+            for (int i = seqPos + 1; i < length; i++) {
+                suffix.append(values.getDataAt(i));
+                if (i < length - 1) {
+                    suffix.append(sep);
+                }
+            }
         }
         return RDataFactory.createStringSequence(prefix.toString(), suffix.toString(), seq.getStart(), seq.getStride(), seq.getLength());
     }
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_paste.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_paste.java
index 197cd5a603..43252d69aa 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_paste.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_paste.java
@@ -4,7 +4,7 @@
  * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * Copyright (c) 2012-2014, Purdue University
- * Copyright (c) 2013, 2017, Oracle and/or its affiliates
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates
  *
  * All rights reserved.
  */
@@ -84,6 +84,12 @@ public class TestBuiltin_paste extends TestBase {
         assertEval("{ paste(NA) }");
         assertEval("{ paste(c(1,NA)) }");
         assertEval("{ s<-c('1',NA); paste(s); s; }");
+
+        assertEval("{ paste(1:3, sep='.') }");
+        assertEval("{ paste('a', 1:3, sep='.') }");
+        assertEval("{ paste(1:3, 'b', sep='.') }");
+        assertEval("{ paste('a', 1:3, 'b', sep='.') }");
+        assertEval("{ paste('a', 'a', 1:3, 'b', 'b', sep='.') }");
     }
 
     @Test
-- 
GitLab