Skip to content

Commit 0cc11e2

Browse files
committed
fix for new null-terminated UTF16Strings
1 parent 7fc0d93 commit 0cc11e2

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

src/ICU.jl

+22-27
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function utext_open(s::UTF16String)
170170
err = UErrorCode[0]
171171
p = ccall((_utext_openUChars,iculib), Ptr{Void},
172172
(Ptr{Void},Ptr{Uint16},Int64,Ptr{UErrorCode}),
173-
C_NULL, s.data, length(s.data), err)
173+
C_NULL, s.data, length(s.data)-1, err)
174174
@assert U_SUCCESS(err[1])
175175
UText(p)
176176
end
@@ -188,9 +188,9 @@ for f in [:u_strToLower, :u_strToUpper]
188188
err = UErrorCode[0]
189189
n = ccall(($(symbol(string('_',f))),iculib), Int32,
190190
(Ptr{Uint16},Int32,Ptr{Uint16},Int32,Ptr{Uint8},Ptr{UErrorCode}),
191-
dest, destsiz, src, length(src), locale, err)
191+
dest, destsiz, src, length(src)-1, locale, err)
192192
U_FAILURE(err[1]) && error("failed to map case")
193-
return UTF16String(dest[1:n])
193+
return UTF16String(dest[1:n+1])
194194
end
195195
end
196196
end
@@ -202,9 +202,9 @@ function u_strFoldCase(s::UTF16String)
202202
err = UErrorCode[0]
203203
n = ccall((_u_strFoldCase,iculib), Int32,
204204
(Ptr{Uint16},Int32,Ptr{Uint16},Int32,Uint32,Ptr{UErrorCode}),
205-
dest, destsiz, src, length(src), 0, err)
205+
dest, destsiz, src, length(src)-1, 0, err)
206206
U_FAILURE(err[1]) && error("failed to map case")
207-
return UTF16String(dest[1:n])
207+
return UTF16String(dest[1:n+1])
208208
end
209209

210210
function u_strToTitle(s::UTF16String)
@@ -216,9 +216,9 @@ function u_strToTitle(s::UTF16String)
216216
Ptr{Void}, (Ptr{Void},), casemap)
217217
n = ccall((_u_strToTitle,iculib), Int32,
218218
(Ptr{Uint16},Int32,Ptr{Uint16},Int32,Ptr{Void},Ptr{Uint8},Ptr{UErrorCode}),
219-
dest, destsiz, src, length(src), breakiter, locale, err)
219+
dest, destsiz, src, length(src)-1, breakiter, locale, err)
220220
U_FAILURE(err[1]) && error("failed to map case")
221-
return UTF16String(dest[1:n])
221+
return UTF16String(dest[1:n+1])
222222
end
223223

224224
## ucasemap ##
@@ -344,13 +344,13 @@ function ucnv_open(name::ASCIIString)
344344
end
345345

346346
function ucnv_toUChars(cnv::UConverter, b::Array{Uint8,1})
347-
u = Array(Uint16, 2*length(b))
347+
u = zeros(Uint16, 2*length(b))
348348
err = UErrorCode[0]
349349
n = ccall((_ucnv_toUChars,iculibi18n), Int32,
350350
(Ptr{Void},Ptr{UChar},Int32,Ptr{Cchar},Int32,Ptr{UErrorCode}),
351351
cnv.p, u, length(u), b, length(b), err)
352352
U_SUCCESS(err[1]) || error("ICU: could not open converter ", name)
353-
UTF16String(u[1:n])
353+
UTF16String(u[1:n+1])
354354
end
355355

356356
## ucol ##
@@ -370,7 +370,7 @@ function ucol_open(loc::LocaleString)
370370
UCollator(p)
371371
end
372372

373-
function ucol_strcoll(c::UCollator, a::Array{Uint8,1}, b::Array{Uint8,1})
373+
function ucol_strcoll(c::UCollator, a::UTF8String, b::UTF8String)
374374
err = UErrorCode[0]
375375
o = ccall((_ucol_strcollUTF8,iculibi18n), Int32,
376376
(Ptr{Void},Ptr{Uint8},Int32,Ptr{Uint8},Int32,Ptr{UErrorCode}),
@@ -379,20 +379,15 @@ function ucol_strcoll(c::UCollator, a::Array{Uint8,1}, b::Array{Uint8,1})
379379
o
380380
end
381381

382-
function ucol_strcoll(c::UCollator, a::Array{Uint16,1}, b::Array{Uint16,1})
382+
function ucol_strcoll(c::UCollator, a::UTF16String, b::UTF16String)
383383
err = UErrorCode[0]
384384
o = ccall((_ucol_strcoll,iculibi18n), Int32,
385385
(Ptr{Void},Ptr{Uint16},Int32,Ptr{Uint16},Int32,Ptr{UErrorCode}),
386-
c.p, a, length(a), b, length(b), err)
386+
c.p, a.data, length(a.data)-1, b.data, length(b.data)-1, err)
387387
@assert U_SUCCESS(err[1])
388388
o
389389
end
390390

391-
ucol_strcoll(c::UCollator, a::UTF8String, b::UTF8String) =
392-
ucol_strcoll(c, a.data, b.data)
393-
ucol_strcoll(c::UCollator, a::UTF16String, b::UTF16String) =
394-
ucol_strcoll(c, a.data, b.data)
395-
396391
## calendar ##
397392

398393
export ICUCalendar,
@@ -472,7 +467,7 @@ function ICUCalendar(timezone::String)
472467
err = UErrorCode[0]
473468
p = ccall((_ucal_open,iculibi18n), Ptr{Void},
474469
(Ptr{Uint16},Int32,Ptr{Uint8},Int32,Ptr{UErrorCode}),
475-
tz_u16.data, length(tz_u16.data), locale, 0, err)
470+
tz_u16.data, length(tz_u16.data)-1, locale, 0, err)
476471
ICUCalendar(p)
477472
end
478473
function ICUCalendar()
@@ -541,21 +536,21 @@ end
541536

542537
function getTimeZoneDisplayName(cal::ICUCalendar)
543538
bufsz = 64
544-
buf = Array(Uint16, bufsz)
539+
buf = zeros(Uint16, bufsz)
545540
err = UErrorCode[0]
546541
len = ccall((_ucal_getTimeZoneDisplayName,iculibi18n), Int32,
547542
(Ptr{Void},Int32,Ptr{Uint8},Ptr{UChar},Int32,Ptr{UErrorCode}),
548543
cal.ptr, 1, locale, buf, bufsz, err)
549-
UTF16String(buf[1:len])
544+
UTF16String(buf[1:len+1])
550545
end
551546

552547
function getDefaultTimeZone()
553548
bufsz = 64
554-
buf = Array(Uint16, bufsz)
549+
buf = zeros(Uint16, bufsz)
555550
err = UErrorCode[0]
556551
len = ccall((_ucal_getDefaultTimeZone,iculibi18n), Int32,
557552
(Ptr{UChar},Int32,Ptr{UErrorCode}), buf, bufsz, err)
558-
UTF16String(buf[1:len])
553+
UTF16String(buf[1:len+1])
559554
end
560555

561556
export ICUDateFormat,
@@ -587,8 +582,8 @@ function ICUDateFormat(pattern::String, tz::String)
587582
err = UErrorCode[0]
588583
p = ccall((_udat_open,iculibi18n), Ptr{Void},
589584
(Int32, Int32, Ptr{Uint8}, Ptr{UChar}, Int32, Ptr{UChar}, Int32, Ptr{UErrorCode}),
590-
-2, -2, locale, tz_u16.data, length(tz_u16.data),
591-
pattern_u16.data, length(pattern_u16.data), err)
585+
-2, -2, locale, tz_u16.data, length(tz_u16.data)-1,
586+
pattern_u16.data, length(pattern_u16.data)-1, err)
592587
U_FAILURE(err[1]) && error("bad date format")
593588
ICUDateFormat(p)
594589
end
@@ -597,7 +592,7 @@ function ICUDateFormat(tstyle::Integer, dstyle::Integer, tz::String)
597592
err = UErrorCode[0]
598593
p = ccall((_udat_open,iculibi18n), Ptr{Void},
599594
(Int32, Int32, Ptr{Uint8}, Ptr{UChar}, Int32, Ptr{UChar}, Int32, Ptr{UErrorCode}),
600-
tstyle, dstyle, locale, tz_u16.data, length(tz_u16.data), C_NULL, -1, err)
595+
tstyle, dstyle, locale, tz_u16.data, length(tz_u16.data)-1, C_NULL, -1, err)
601596
U_FAILURE(err[1]) && error("bad date format")
602597
ICUDateFormat(p)
603598
end
@@ -613,15 +608,15 @@ function format(df::ICUDateFormat, millis::Float64)
613608
(Ptr{Void}, Float64, Ptr{UChar}, Int32, Ptr{Void}, Ptr{UErrorCode}),
614609
df.ptr, millis, buf, buflen, C_NULL, err)
615610
U_FAILURE(err[1]) && error("failed to format time")
616-
UTF16String(buf[1:len])
611+
UTF16String(buf[1:len+1])
617612
end
618613

619614
function parse(df::ICUDateFormat, s::String)
620615
s16 = utf16(s)
621616
err = UErrorCode[0]
622617
ret = ccall((_udat_parse,iculibi18n), Float64,
623618
(Ptr{Void}, Ptr{UChar}, Int32, Ptr{Int32}, Ptr{UErrorCode}),
624-
df.ptr, s16.data, length(s16.data), C_NULL, err)
619+
df.ptr, s16.data, length(s16.data)-1, C_NULL, err)
625620
U_FAILURE(err[1]) && error("failed to parse string")
626621
ret
627622
end

0 commit comments

Comments
 (0)