Skip to content

Commit 3fca948

Browse files
[MIN] fn:sequence-join → fn:insert-separator
1 parent 509c133 commit 3fca948

File tree

5 files changed

+71
-58
lines changed

5 files changed

+71
-58
lines changed

basex-core/src/main/java/org/basex/query/func/Function.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ ITEM_ZM, flag(HOF)),
394394
/** XQuery function. */
395395
INSERT_BEFORE(FnInsertBefore::new, "insert-before(input,position,insert)",
396396
params(ITEM_ZM, INTEGER_O, ITEM_ZM), ITEM_ZM),
397+
/** XQuery function. */
398+
INSERT_SEPARATOR(FnInsertSeparator::new, "insert-separator(input,separator)",
399+
params(ITEM_ZM, ITEM_ZM), ITEM_ZM),
397400
/** XQuery function (obsolete). */
398401
INTERSPERSE(FnIntersperse::new, "intersperse(input,separator)",
399402
params(ITEM_ZM, ITEM_ZM), ITEM_ZM),
@@ -616,7 +619,7 @@ ARRAY_ZM, flag(HOF)),
616619
/** XQuery function. */
617620
SECONDS_FROM_TIME(FnSecondsFromTime::new, "seconds-from-time(value)",
618621
params(TIME_ZO), DECIMAL_ZO),
619-
/** XQuery function. */
622+
/** XQuery function (obsolete). */
620623
SEQUENCE_JOIN(FnSequenceJoin::new, "sequence-join(input,separator)",
621624
params(ITEM_ZM, ITEM_ZM), ITEM_ZM),
622625
/** XQuery function. */
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.basex.query.func.fn;
2+
3+
import org.basex.query.*;
4+
import org.basex.query.expr.*;
5+
import org.basex.query.func.*;
6+
import org.basex.query.iter.*;
7+
import org.basex.query.value.*;
8+
import org.basex.query.value.item.*;
9+
import org.basex.query.value.seq.*;
10+
import org.basex.query.value.type.*;
11+
12+
/**
13+
* Function implementation.
14+
*
15+
* @author BaseX Team, BSD License
16+
* @author Christian Gruen
17+
*/
18+
public class FnInsertSeparator extends StandardFunc {
19+
@Override
20+
public final Value value(final QueryContext qc) throws QueryException {
21+
final Iter input = arg(0).iter(qc);
22+
final Value separator = arg(1).value(qc);
23+
24+
final ValueBuilder vb = new ValueBuilder(qc);
25+
Item item = input.next();
26+
if(item != null) {
27+
vb.add(item);
28+
while((item = qc.next(input)) != null) vb.add(separator).add(item);
29+
}
30+
return vb.value(this);
31+
}
32+
33+
@Override
34+
protected final Expr opt(final CompileContext cc) {
35+
final Expr values = arg(0), separator = arg(1);
36+
final SeqType st = values.seqType(), stSep = separator.seqType();
37+
if(st.zeroOrOne() || separator == Empty.VALUE) return values;
38+
39+
final long size = values.size(), sizeSep = separator.size();
40+
final long sz = size != -1 && sizeSep != -1 ? size + sizeSep * (size - 1) : -1;
41+
exprType.assign(st.union(stSep), st.occ, sz).data(values, separator);
42+
43+
return this;
44+
}
45+
}

basex-core/src/main/java/org/basex/query/func/fn/FnIntersperse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
* @author BaseX Team, BSD License
77
* @author Christian Gruen
88
*/
9-
public final class FnIntersperse extends FnSequenceJoin {
9+
public final class FnIntersperse extends FnInsertSeparator {
1010
}
Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,10 @@
11
package org.basex.query.func.fn;
22

3-
import org.basex.query.*;
4-
import org.basex.query.expr.*;
5-
import org.basex.query.func.*;
6-
import org.basex.query.iter.*;
7-
import org.basex.query.value.*;
8-
import org.basex.query.value.item.*;
9-
import org.basex.query.value.seq.*;
10-
import org.basex.query.value.type.*;
11-
123
/**
134
* Function implementation.
145
*
156
* @author BaseX Team, BSD License
167
* @author Christian Gruen
178
*/
18-
public class FnSequenceJoin extends StandardFunc {
19-
@Override
20-
public final Value value(final QueryContext qc) throws QueryException {
21-
final Iter input = arg(0).iter(qc);
22-
final Value separator = arg(1).value(qc);
23-
24-
final ValueBuilder vb = new ValueBuilder(qc);
25-
Item item = input.next();
26-
if(item != null) {
27-
vb.add(item);
28-
while((item = qc.next(input)) != null) vb.add(separator).add(item);
29-
}
30-
return vb.value(this);
31-
}
32-
33-
@Override
34-
protected final Expr opt(final CompileContext cc) {
35-
final Expr values = arg(0), separator = arg(1);
36-
final SeqType st = values.seqType(), stSep = separator.seqType();
37-
if(st.zeroOrOne() || separator == Empty.VALUE) return values;
38-
39-
final long size = values.size(), sizeSep = separator.size();
40-
final long sz = size != -1 && sizeSep != -1 ? size + sizeSep * (size - 1) : -1;
41-
exprType.assign(st.union(stSep), st.occ, sz).data(values, separator);
42-
43-
return this;
44-
}
9+
public final class FnSequenceJoin extends FnInsertSeparator {
4510
}

basex-core/src/test/java/org/basex/query/func/FnModuleTest.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,26 @@ public final class FnModuleTest extends SandboxTest {
16151615
"56zyx\n56zyx\n5zyx6\nzyx56\nzyx56");
16161616
}
16171617

1618+
/** Test method. */
1619+
@Test public void insertSeparator() {
1620+
final Function func = INSERT_SEPARATOR;
1621+
1622+
query(func.args(" ()", " ()"), "");
1623+
query(func.args(" ()", 1), "");
1624+
query(func.args(1, " ()"), 1);
1625+
query(func.args(" (1, 2)", " ()"), "1\n2");
1626+
1627+
query(func.args(1, "a"), 1);
1628+
query(func.args(1, " ('a', 'b')"), 1);
1629+
query(func.args(" (1, 2)", "a"), "1\na\n2");
1630+
query(func.args(" (1, 2)", " ('a', 'b')"), "1\na\nb\n2");
1631+
1632+
check(func.args(1, "a") + " => count()", 1, root(Itr.class));
1633+
check(func.args(" 1[. = <_>1</_>]", "a"), 1, root(If.class));
1634+
check(func.args(" (1, 2)[. = <_>3</_>]", " 'a'"), "", root(func));
1635+
check(func.args(" (1, 2)", " 'a'[. = <_/>]"), "1\n2", root(func));
1636+
}
1637+
16181638
/** Test method. */
16191639
@Test public void invisibleXml() {
16201640
final Function func = INVISIBLE_XML;
@@ -2906,26 +2926,6 @@ public final class FnModuleTest extends SandboxTest {
29062926
query(func.args(" #xs:numeric") + " ? members() ? name", "#double\n#float\n#decimal");
29072927
}
29082928

2909-
/** Test method. */
2910-
@Test public void sequenceJoin() {
2911-
final Function func = SEQUENCE_JOIN;
2912-
2913-
query(func.args(" ()", " ()"), "");
2914-
query(func.args(" ()", 1), "");
2915-
query(func.args(1, " ()"), 1);
2916-
query(func.args(" (1, 2)", " ()"), "1\n2");
2917-
2918-
query(func.args(1, "a"), 1);
2919-
query(func.args(1, " ('a', 'b')"), 1);
2920-
query(func.args(" (1, 2)", "a"), "1\na\n2");
2921-
query(func.args(" (1, 2)", " ('a', 'b')"), "1\na\nb\n2");
2922-
2923-
check(func.args(1, "a") + " => count()", 1, root(Itr.class));
2924-
check(func.args(" 1[. = <_>1</_>]", "a"), 1, root(If.class));
2925-
check(func.args(" (1, 2)[. = <_>3</_>]", " 'a'"), "", root(func));
2926-
check(func.args(" (1, 2)", " 'a'[. = <_/>]"), "1\n2", root(func));
2927-
}
2928-
29292929
/** Test method. */
29302930
@Test public void serialize() {
29312931
final Function func = SERIALIZE;

0 commit comments

Comments
 (0)