Repository navigation
Commit a68c74b
authored
Fix XPath substring() with a start before the first character and round() for negative halves (#382)
`substring()` drops characters when the start is before the first
character, and `round()` rounds negative halves the wrong way.
| query | master | this PR | libxml2 |
|---|---|---|---|
| `substring('12345', 0)` | `1234` | `12345` | `12345` |
| `substring('12345', -1)` | `123` | `12345` | `12345` |
| `substring('12345', -1 div 0)` | (empty) | `12345` | `12345` |
| `substring('12345', -0.5, 1.5)` | (empty) | `1` | `1` |
| `round(-2.5)` | `-3` | `-2` | `-2` |
| `round(-0.5)` | `-1` | `-0` | `-0` |
| `1 div round(-0.4)` | `Infinity` | `-Infinity` | `-Infinity` |
Cause:
* The two-argument form set the length to the string length and then
subtracted the start offset from it (`ruby_length += ruby_start`), so
every position before 1 cut one more character off the end. A
`-Infinity` start returned `''` right away.
* `round()` rounded `abs(n)` with Ruby's half-away-from-zero rule and
put the sign back, so `-2.5` became `-3`.
https://www.w3.org/TR/xpath-10/#function-substring
> More precisely, each character in the string is considered to have a
numeric position: the position of the first character is 1, the position
of the second character is 2 and so on. [...] The returned substring
contains those characters for which the position of the character is
greater than or equal to the rounded value of the second argument and,
if the third argument is specified, less than the sum of the rounded
value of the second argument and the rounded value of the third argument
https://www.w3.org/TR/xpath-10/#function-round
> The round function returns the number that is closest to the argument
and that is an integer. If there are two such numbers, then the one that
is closest to positive infinity is returned. [...] If the argument is
less than zero, but greater than or equal to -0.5, then negative zero is
returned.
`substring()` now follows that rule directly: keep positions `p` with
`round(start) <= p < round(start) + round(length)` (no upper bound when
there is no length). NaN bounds still give `''`, so the existing spec
examples (`substring("12345", 0 div 0, 3)`, `substring("12345", -1 div
0, 1 div 0)`, ...) keep passing.1 parent 121633d commit a68c74b
2 files changed
Lines changed: 48 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
232 | 232 | | |
233 | 233 | | |
234 | 234 | | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
260 | 253 | | |
261 | | - | |
262 | | - | |
263 | 254 | | |
264 | 255 | | |
265 | 256 | | |
| |||
431 | 422 | | |
432 | 423 | | |
433 | 424 | | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
441 | 433 | | |
442 | 434 | | |
443 | 435 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
180 | 191 | | |
181 | 192 | | |
182 | 193 | | |
| |||
207 | 218 | | |
208 | 219 | | |
209 | 220 | | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
210 | 232 | | |
211 | 233 | | |
212 | 234 | | |
| |||
0 commit comments