Skip to content

Commit 94b45cb

Browse files
chris-evolvedbinaryadamretter
authored andcommitted
[bugfix] Fix binary-value equality checks.
1 parent 2e7733b commit 94b45cb

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/FunCollationKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
6565
final Collator collator = (args.length >= 2) ? Collations.getCollationFromURI(args[1].itemAt(0).toString()) : null;
6666

6767
return new BinaryValueFromBinaryString(new Base64BinaryValueType(), Base64.encodeBase64String(
68-
(collator == null) ? source.getBytes(StandardCharsets.UTF_8) : new String(collator.getCollationKey(source).toByteArray()).getBytes(StandardCharsets.UTF_8)));
68+
(collator == null) ? source.getBytes(StandardCharsets.UTF_8) : new String(collator.getCollationKey(source).toByteArray()).getBytes(StandardCharsets.UTF_8))).convertTo(new Base64BinaryValueType());
6969
}
7070

7171
private static FunctionSignature[] functionSignatures(final String name, final String description, final FunctionReturnSequenceType returnType, final FunctionParameterSequenceType[][] variableParamTypes) {

exist-core/src/main/java/org/exist/xquery/value/BinaryValue.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.exist.xquery.XPathException;
3232

3333
import java.io.*;
34+
import java.util.Objects;
3435

3536
import static java.nio.charset.StandardCharsets.UTF_8;
3637

@@ -109,10 +110,13 @@ private int compareTo(BinaryValue otherValue) {
109110
return -1;
110111
} else if (otherIs == null) {
111112
return 1;
113+
} else if (is == otherIs) {
114+
return 0;
112115
} else {
113-
int read = -1;
114-
int otherRead = -1;
115-
while (true) {
116+
int read;
117+
int otherRead;
118+
do {
119+
116120
try {
117121
read = is.read();
118122
} catch (final IOException ioe) {
@@ -125,8 +129,12 @@ private int compareTo(BinaryValue otherValue) {
125129
return 1;
126130
}
127131

128-
return read - otherRead;
129-
}
132+
final int readComparison = Integer.compare(read, otherRead);
133+
if (readComparison != 0) {
134+
return readComparison;
135+
}
136+
} while (read != -1 && otherRead != -1);
137+
return 0;
130138
}
131139
}
132140

@@ -276,4 +284,12 @@ public void streamTo(OutputStream os) throws IOException {
276284
* Increments the number of shared references to this binary value.
277285
*/
278286
public abstract void incrementSharedReferences();
287+
288+
@Override
289+
public boolean equals(Object o) {
290+
if (this == o) return true;
291+
if (o == null || getClass() != o.getClass()) return false;
292+
BinaryValue that = (BinaryValue) o;
293+
return compareTo(that) == 0;
294+
}
279295
}

exist-core/src/test/xquery/xquery3/fnCollationKey.xqm

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ function fnck:exist-equal() {
6767

6868
declare
6969
%test:assertTrue
70-
function fnck:exist-equal-ignore-case() {
70+
function fnck:exist-not-equal() {
7171
let $first := fn:collation-key("a", "http://exist-db.org/collation")
72-
let $second := fn:collation-key("A", "http://exist-db.org/collation")
73-
return $first eq $second
72+
let $second := fn:collation-key("b", "http://exist-db.org/collation")
73+
return $first ne $second
7474
};
7575

7676
declare
7777
%test:assertTrue
78-
function fnck:exist-not-equal() {
78+
function fnck:exist-not-equal-ignore-case() {
7979
let $first := fn:collation-key("a", "http://exist-db.org/collation")
80-
let $second := fn:collation-key("b", "http://exist-db.org/collation")
80+
let $second := fn:collation-key("A", "http://exist-db.org/collation")
8181
return $first ne $second
8282
};
8383

@@ -90,16 +90,16 @@ function fnck:invalid-uri() {
9090
declare
9191
%test:assertTrue
9292
function fnck:uca-equal() {
93-
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA")
94-
let $second := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA")
93+
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA?lang=en;strength=primary")
94+
let $second := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA?lang=en;strength=primary")
9595
return $first eq $second
9696
};
9797

9898
declare
9999
%test:assertTrue
100100
function fnck:uca-equal-ignore-case() {
101-
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA")
102-
let $second := fn:collation-key("A", "http://www.w3.org/2013/collation/UCA")
101+
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA?lang=en;strength=primary")
102+
let $second := fn:collation-key("A", "http://www.w3.org/2013/collation/UCA?lang=en;strength=primary")
103103
return $first eq $second
104104
};
105105

@@ -108,5 +108,5 @@ declare
108108
function fnck:uca-not-equal() {
109109
let $first := fn:collation-key(fn:codepoints-to-string((37, 65500, 37)))
110110
let $second := fn:collation-key(fn:codepoints-to-string((37, 100000, 37)))
111-
return $first eq $second
111+
return $first lt $second
112112
};

0 commit comments

Comments
 (0)