From 4813b2925b3ecad071ec0f42f9e4b6f7ac04c87a Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Sat, 28 Mar 2015 14:50:15 -0400 Subject: [PATCH] update libmojibake -> utf8proc 1.2 (closes #10654); replace wcwidth by utf8proc_charwidth (fixes #3721, closes #6939) --- .gitmodules | 6 +- Make.inc | 14 +- base/string.jl | 1 - base/utf8proc.jl | 17 +- contrib/windows/msys_build.sh | 8 +- deps/Makefile | 62 +++---- deps/libmojibake | 1 - deps/utf8proc | 1 + src/Makefile | 2 +- src/flisp/Makefile | 4 +- src/flisp/Windows.mk | 10 +- src/flisp/julia_extensions.c | 4 +- src/flisp/string.c | 12 +- src/julia.expmap | 1 - src/support/Makefile | 4 +- src/support/Windows.mk | 3 +- src/support/utf8.c | 13 +- src/support/wcwidth.c | 323 ---------------------------------- test/unicode.jl | 3 + 19 files changed, 81 insertions(+), 408 deletions(-) delete mode 160000 deps/libmojibake create mode 160000 deps/utf8proc delete mode 100644 src/support/wcwidth.c diff --git a/.gitmodules b/.gitmodules index ae14e02ecc1656..95d4bccf7cd168 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,6 +7,6 @@ [submodule "deps/openspecfun"] path = deps/openspecfun url = git://github.com/JuliaLang/openspecfun.git -[submodule "deps/libmojibake"] - path = deps/libmojibake - url = git://github.com/JuliaLang/libmojibake.git +[submodule "deps/utf8proc"] + path = deps/utf8proc + url = git://github.com/JuliaLang/utf8proc.git diff --git a/Make.inc b/Make.inc index ccf6cbd9cceac0..a4d549887defb1 100644 --- a/Make.inc +++ b/Make.inc @@ -35,7 +35,7 @@ USE_SYSTEM_SUITESPARSE=0 USE_SYSTEM_RMATH=0 USE_SYSTEM_LIBUV=0 USE_SYSTEM_UTF8PROC=0 -USE_SYSTEM_MOJIBAKE=0 +USE_SYSTEM_UTF8PROC=0 USE_SYSTEM_LIBGIT2=0 # Link to the LLVM shared library @@ -594,14 +594,12 @@ else LIBUV_INC = $(JULIAHOME)/deps/libuv/include endif -ifeq ($(USE_SYSTEM_MOJIBAKE), 1) - LIBMOJIBAKE = -lmojibake +ifeq ($(USE_SYSTEM_UTF8PROC), 1) + LIBUTF8PROC = -lutf8proc + UTF8PROC_INC = /usr/include else - ifeq ($(USE_SYSTEM_UTF8PROC), 1) - LIBMOJIBAKE = -lutf8proc - else - LIBMOJIBAKE = $(build_libdir)/libmojibake.a - endif + LIBUTF8PROC = $(build_libdir)/libutf8proc.a + UTF8PROC_INC = $(JULIAHOME)/deps/utf8proc endif # OS specific stuff diff --git a/base/string.jl b/base/string.jl index 888580f5e4be8c..8d991f1510a2e2 100644 --- a/base/string.jl +++ b/base/string.jl @@ -541,7 +541,6 @@ startswith(a::Array{UInt8,1}, b::Array{UInt8,1}) = ## character column width function ## -charwidth(c::Char) = max(0,Int(ccall(:wcwidth, Int32, (UInt32,), c))) strwidth(s::AbstractString) = (w=0; for c in s; w += charwidth(c); end; w) strwidth(s::ByteString) = Int(ccall(:u8_strwidth, Csize_t, (Ptr{UInt8},), s.data)) # TODO: implement and use u8_strnwidth that takes a length argument diff --git a/base/utf8proc.jl b/base/utf8proc.jl index e58444d4ae0ad4..d0b4f7daa58ec9 100644 --- a/base/utf8proc.jl +++ b/base/utf8proc.jl @@ -6,13 +6,13 @@ import Base: show, showcompact, ==, hash, string, symbol, isless, length, eltype export isgraphemebreak # also exported by Base: -export normalize_string, graphemes, is_valid_char, is_assigned_char, +export normalize_string, graphemes, is_valid_char, is_assigned_char, charwidth, islower, isupper, isalpha, isdigit, isnumber, isalnum, iscntrl, ispunct, isspace, isprint, isgraph, isblank # whether codepoints are valid Unicode -is_valid_char(c) = (0x0 <= c <= 0x110000) && Bool(ccall(:utf8proc_codepoint_valid, Cuchar, (Int32,), c)) -is_valid_char(c::Char) = is_valid_char(UInt32(c)) +is_valid_char(c::Union(UInt8,UInt16,UInt32,Char)) = Bool(ccall(:utf8proc_codepoint_valid, Cuchar, (UInt32,), c)) +is_valid_char(c::Integer) = (0x0 <= c <= 0x110000) && is_valid_char(UInt32(c)) # utf8 category constants const UTF8PROC_CATEGORY_CN = 0 @@ -116,10 +116,13 @@ end ############################################################################ -# returns UTF8PROC_CATEGORY code in 1:30 giving Unicode category +charwidth(c::Char) = Int(ccall(:utf8proc_charwidth, Cint, (UInt32,), c)) + +############################################################################ + +# returns UTF8PROC_CATEGORY code in 0:30 giving Unicode category function category_code(c) - UInt32(c) > 0x10FFFF && return 0x0000 # see utf8proc_get_property docs - return unsafe_load(ccall(:utf8proc_get_property, Ptr{UInt16}, (Int32,), c)) + return ccall(:utf8proc_category, Cint, (UInt32,), c) end is_assigned_char(c) = category_code(c) != UTF8PROC_CATEGORY_CN @@ -176,7 +179,7 @@ end # iterators for grapheme segmentation isgraphemebreak(c1::Char, c2::Char) = - ccall(:utf8proc_grapheme_break, Bool, (Char, Char), c1, c2) + ccall(:utf8proc_grapheme_break, Bool, (UInt32, UInt32), c1, c2) immutable GraphemeIterator{S<:AbstractString} s::S # original string (for generation of SubStrings) diff --git a/contrib/windows/msys_build.sh b/contrib/windows/msys_build.sh index 7ca54d96aaaee2..9f6f550e5e3b47 100755 --- a/contrib/windows/msys_build.sh +++ b/contrib/windows/msys_build.sh @@ -180,9 +180,9 @@ echo 'override LIBLAPACKNAME = $(LIBBLASNAME)' >> Make.user # Remaining dependencies: # libuv since its static lib is no longer included in the binaries # openlibm since we need it as a static library to work properly -# mojibake since its headers are not in the binary download +# utf8proc since its headers are not in the binary download echo 'override STAGE1_DEPS = uv' >> Make.user -echo 'override STAGE2_DEPS = mojibake' >> Make.user +echo 'override STAGE2_DEPS = utf8proc' >> Make.user echo 'override STAGE3_DEPS = ' >> Make.user make -C deps get-uv @@ -197,8 +197,8 @@ if [ -n "$USEMSVC" ]; then # Since we don't have a static library for openlibm echo 'override UNTRUSTED_SYSTEM_LIBM = 0' >> Make.user - # Compile libuv and mojibake without -TP first, then add -TP - make -C deps install-uv install-mojibake + # Compile libuv and utf8proc without -TP first, then add -TP + make -C deps install-uv install-utf8proc cp usr/lib/uv.lib usr/lib/libuv.a echo 'override CC += -TP' >> Make.user else diff --git a/deps/Makefile b/deps/Makefile index a48e8b28385ddf..9c762d1ce8623f 100644 --- a/deps/Makefile +++ b/deps/Makefile @@ -11,10 +11,10 @@ include $(JULIAHOME)/Make.inc # if you are adding a new target, it can help to copy an similar, existing target # # autoconf configure-driven scripts: llvm pcre arpack fftw unwind gmp mpfr patchelf uv -# custom Makefile rules: openlibm Rmath-julia dsfmt suitesparse-wrapper suitesparse lapack openblas mojibake objconv +# custom Makefile rules: openlibm Rmath-julia dsfmt suitesparse-wrapper suitesparse lapack openblas utf8proc objconv # CMake libs: libgit2 # -# downloaded from git: llvm-svn, uv, libopenlibm, mojibake, openspecfun +# downloaded from git: llvm-svn, uv, libopenlibm, utf8proc, openspecfun # # there are rules in this file with the . replaced by a % # this is some magic Makefile trick that tells make @@ -150,8 +150,8 @@ ifeq ($(USE_SYSTEM_SUITESPARSE), 0) STAGE2_DEPS += suitesparse endif -ifeq ($(USE_SYSTEM_MOJIBAKE), 0) -STAGE2_DEPS += mojibake +ifeq ($(USE_SYSTEM_UTF8PROC), 0) +STAGE2_DEPS += utf8proc endif # Only compile standalone LAPACK if we are not using OpenBLAS. @@ -183,7 +183,7 @@ install: $(addprefix install-, $(DEP_LIBS)) cleanall: $(addprefix clean-, $(DEP_LIBS)) distcleanall: $(addprefix distclean-, $(DEP_LIBS)) rm -rf $(build_prefix) -getall: get-llvm get-uv get-pcre get-openlibm get-openspecfun get-dsfmt get-Rmath-julia get-openblas get-lapack get-fftw get-suitesparse get-arpack get-unwind get-osxunwind get-gmp get-mpfr get-patchelf get-mojibake get-virtualenv get-objconv get-libgit2 +getall: get-llvm get-uv get-pcre get-openlibm get-openspecfun get-dsfmt get-Rmath-julia get-openblas get-lapack get-fftw get-suitesparse get-arpack get-unwind get-osxunwind get-gmp get-mpfr get-patchelf get-utf8proc get-virtualenv get-objconv get-libgit2 ## PATHS ## # sort is used to remove potential duplicates @@ -1344,47 +1344,47 @@ compile-fftw-double: $(FFTW_DOUBLE_OBJ_TARGET) check-fftw-double: fftw-$(FFTW_VER)-double/checked install-fftw-double: $(FFTW_DOUBLE_OBJ_TARGET) -## MOJIBAKE ## +## UTF8PROC ## -MOJIBAKE_SRC_TARGET = libmojibake/libmojibake.a -MOJIBAKE_OBJ_LIB = $(build_libdir)/libmojibake.a -MOJIBAKE_OBJ_HEADER = $(build_includedir)/mojibake.h -MOJIBAKE_OBJ_TARGET = $(MOJIBAKE_OBJ_LIB) $(MOJIBAKE_OBJ_HEADER) +UTF8PROC_SRC_TARGET = utf8proc/libutf8proc.a +UTF8PROC_OBJ_LIB = $(build_libdir)/libutf8proc.a +UTF8PROC_OBJ_HEADER = $(build_includedir)/utf8proc.h +UTF8PROC_OBJ_TARGET = $(UTF8PROC_OBJ_LIB) $(UTF8PROC_OBJ_HEADER) -libmojibake/Makefile: +utf8proc/Makefile: (cd .. && git submodule init && git submodule update) -ifeq (exists, $(shell [ -d libmojibake/.git ] && echo exists )) -$(MOJIBAKE_SRC_TARGET): libmojibake/.git/HEAD +ifeq (exists, $(shell [ -d utf8proc/.git ] && echo exists )) +$(UTF8PROC_SRC_TARGET): utf8proc/.git/HEAD endif -ifeq (exists, $(shell [ -d $(JULIAHOME)/.git/modules/deps/libmojibake ] && echo exists )) -$(MOJIBAKE_SRC_TARGET): $(JULIAHOME)/.git/modules/deps/libmojibake/HEAD +ifeq (exists, $(shell [ -d $(JULIAHOME)/.git/modules/deps/utf8proc ] && echo exists )) +$(UTF8PROC_SRC_TARGET): $(JULIAHOME)/.git/modules/deps/utf8proc/HEAD endif -$(MOJIBAKE_SRC_TARGET): libmojibake/Makefile - $(MAKE) -C libmojibake cc="$(CC) -O2 -std=c99 $(fPIC) -DMOJIBAKE_EXPORTS" AR="$(AR)" libmojibake.a +$(UTF8PROC_SRC_TARGET): utf8proc/Makefile + $(MAKE) -C utf8proc cc="$(CC) -O2 -std=c99 $(fPIC) -DUTF8PROC_EXPORTS" AR="$(AR)" libutf8proc.a touch -c $@ -libmojibake/checked: $(MOJIBAKE_SRC_TARGET) +utf8proc/checked: $(UTF8PROC_SRC_TARGET) ifeq ($(OS),$(BUILD_OS)) - -$(MAKE) -C libmojibake check + -$(MAKE) -C utf8proc check endif echo 1 > $@ -$(MOJIBAKE_OBJ_LIB): $(MOJIBAKE_SRC_TARGET) +$(UTF8PROC_OBJ_LIB): $(UTF8PROC_SRC_TARGET) cp -f $< $@ -$(MOJIBAKE_OBJ_HEADER): libmojibake/Makefile - cp -f libmojibake/mojibake.h $@ +$(UTF8PROC_OBJ_HEADER): utf8proc/Makefile + cp -f utf8proc/utf8proc.h $@ -clean-mojibake: - -$(MAKE) -C libmojibake clean - -rm -rf $(build_libdir)/libmojibake.a $(build_includedir)/mojibake.h -distclean-mojibake: clean-mojibake +clean-utf8proc: + -$(MAKE) -C utf8proc clean + -rm -rf $(build_libdir)/libutf8proc.a $(build_includedir)/utf8proc.h +distclean-utf8proc: clean-utf8proc -get-mojibake: libmojibake/Makefile -configure-mojibake: get-mojibake -compile-mojibake: $(MOJIBAKE_SRC_TARGET) -check-mojibake: libmojibake/checked -install-mojibake: $(MOJIBAKE_OBJ_TARGET) +get-utf8proc: utf8proc/Makefile +configure-utf8proc: get-utf8proc +compile-utf8proc: $(UTF8PROC_SRC_TARGET) +check-utf8proc: utf8proc/checked +install-utf8proc: $(UTF8PROC_OBJ_TARGET) ## SUITESPARSE ## diff --git a/deps/libmojibake b/deps/libmojibake deleted file mode 160000 index 86447ad060d6f4..00000000000000 --- a/deps/libmojibake +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 86447ad060d6f4edf01f2a64b9598dfeeb6e6f7d diff --git a/deps/utf8proc b/deps/utf8proc new file mode 160000 index 00000000000000..e1fdad0ca9dc51 --- /dev/null +++ b/deps/utf8proc @@ -0,0 +1 @@ +Subproject commit e1fdad0ca9dc518b429439b6f4eac546a1bdd0de diff --git a/src/Makefile b/src/Makefile index 136374c6e884f4..2d7a32bf001fde 100644 --- a/src/Makefile +++ b/src/Makefile @@ -30,7 +30,7 @@ ifeq ($(USE_LLVM_SHLIB),1) LLVMLINK = $(call exec,$(LLVM_CONFIG) --ldflags) -lLLVM-$(call exec,$(LLVM_CONFIG) --version) endif -COMMON_LIBS = -L$(build_shlibdir) -L$(build_libdir) $(LIBUV) $(LIBMOJIBAKE) $(NO_WHOLE_ARCHIVE) $(LLVMLINK) $(OSLIBS) +COMMON_LIBS = -L$(build_shlibdir) -L$(build_libdir) $(LIBUV) $(LIBUTF8PROC) $(NO_WHOLE_ARCHIVE) $(LLVMLINK) $(OSLIBS) DEBUG_LIBS = $(WHOLE_ARCHIVE) $(BUILDDIR)/flisp/libflisp-debug.a $(WHOLE_ARCHIVE) $(BUILDDIR)/support/libsupport-debug.a $(COMMON_LIBS) RELEASE_LIBS = $(WHOLE_ARCHIVE) $(BUILDDIR)/flisp/libflisp.a $(WHOLE_ARCHIVE) $(BUILDDIR)/support/libsupport.a $(COMMON_LIBS) diff --git a/src/flisp/Makefile b/src/flisp/Makefile index 4739af77dab1e1..fc0fb64e3a3ce3 100644 --- a/src/flisp/Makefile +++ b/src/flisp/Makefile @@ -22,11 +22,11 @@ HEADERS = $(wildcard *.h) $(LIBUV_INC)/uv.h OBJS = $(SRCS:%.c=$(BUILDDIR)/%.o) DOBJS = $(SRCS:%.c=$(BUILDDIR)/%.dbg.obj) LLTDIR = ../support -LLT = $(BUILDDIR)/$(LLTDIR)/libsupport.a $(LIBUV) $(LIBMOJIBAKE) +LLT = $(BUILDDIR)/$(LLTDIR)/libsupport.a $(LIBUV) $(LIBUTF8PROC) FLAGS = -I$(LLTDIR) $(CFLAGS) $(HFILEDIRS:%=-I%) \ -I$(LIBUV_INC) -I$(build_includedir) $(LIBDIRS:%=-L%) \ - -DLIBRARY_EXPORTS -DMOJIBAKE_EXPORTS + -DLIBRARY_EXPORTS -DUTF8PROC_EXPORT ifneq ($(USEMSVC), 1) FLAGS += -Wall -Wno-strict-aliasing -DUSE_COMPUTED_GOTO -fvisibility=hidden endif diff --git a/src/flisp/Windows.mk b/src/flisp/Windows.mk index 2f62b50a5cbc96..9e9b83db129ecd 100644 --- a/src/flisp/Windows.mk +++ b/src/flisp/Windows.mk @@ -29,17 +29,17 @@ OBJECTS = \ dirname.obj LIBUV = $(MAKEDIR)\..\..\deps\libuv\libuv.lib -LIBMOJIBAKE = $(MAKEDIR)\..\..\deps\libmojibake\libmojibake.lib +LIBUTF8PROC = $(MAKEDIR)\..\..\deps\libutf8proc\libutf8proc.lib LIBSUPPORT = $(MAKEDIR)\..\support\libsupport.lib -INCLUDE = $(INCLUDE);$(MAKEDIR)\..\..\deps\libuv\include;$(MAKEDIR)\..\..\deps\libmojibake;$(MAKEDIR)\..\support +INCLUDE = $(INCLUDE);$(MAKEDIR)\..\..\deps\libuv\include;$(MAKEDIR)\..\..\deps\libutf8proc;$(MAKEDIR)\..\support CFLAGS = $(CFLAGS) /Qstd=c99 -D_CRT_SECURE_NO_WARNINGS -DLIBRARY_EXPORTS LFLAGS = $(LFLAGS) kernel32.lib ws2_32.lib psapi.lib advapi32.lib iphlpapi.lib default: $(NAME).exe -$(NAME).exe: lib$(NAME).lib flmain.obj $(LIBSUPPORT) $(LIBUV) $(LIBMOJIBAKE) +$(NAME).exe: lib$(NAME).lib flmain.obj $(LIBSUPPORT) $(LIBUV) $(LIBUTF8PROC) $(LINK) $(LFLAGS) /OUT:$(NAME).exe /PDB:$(NAME).pdb /MAP $** $(LIBSUPPORT): @@ -48,8 +48,8 @@ $(LIBSUPPORT): $(LIBUV): PUSHD $(MAKEDIR)\..\..\deps\libuv && $(MAKE) /NOLOGO /F Windows.mk && POPD -$(LIBMOJIBAKE): - PUSHD $(MAKEDIR)\..\..\deps\libmojibake && cl -nologo /c utf8proc.c && $(AR) /OUT:libmojibake.lib utf8proc.obj && POPD +$(LIBUTF8PROC): + PUSHD $(MAKEDIR)\..\..\deps\libutf8proc && cl -nologo /c utf8proc.c && $(AR) /OUT:libutf8proc.lib utf8proc.obj && POPD lib$(NAME).lib: $(OBJECTS) $(AR) /OUT:lib$(NAME).lib $** diff --git a/src/flisp/julia_extensions.c b/src/flisp/julia_extensions.c index c5d584f10ce309..77d9ba3b523247 100644 --- a/src/flisp/julia_extensions.c +++ b/src/flisp/julia_extensions.c @@ -3,7 +3,9 @@ #include #include #include "flisp.h" -#include "mojibake.h" + +#undef DLLEXPORT /* avoid conflicting definition */ +#include "utf8proc.h" #ifdef __cplusplus extern "C" { diff --git a/src/flisp/string.c b/src/flisp/string.c index 827d0b336b574d..cdc5021b232ea5 100644 --- a/src/flisp/string.c +++ b/src/flisp/string.c @@ -14,10 +14,14 @@ #include #include "flisp.h" + #if !defined(_OS_WINDOWS_) #include #endif /* !_OS_WINDOWS_ */ +#undef DLLEXPORT /* avoid conflicting definition */ +#include "utf8proc.h" + #ifdef __cplusplus extern "C" { #endif @@ -53,19 +57,13 @@ value_t fl_string_count(value_t *args, u_int32_t nargs) return size_wrap(u8_charnum(str+start, stop-start)); } -#if defined(_OS_WINDOWS_) -extern int wcwidth(uint32_t c); -#elif defined(_OS_LINUX_) -extern int wcwidth(wchar_t c); -#endif - value_t fl_string_width(value_t *args, u_int32_t nargs) { argcount("string.width", nargs, 1); if (iscprim(args[0])) { cprim_t *cp = (cprim_t*)ptr(args[0]); if (cp_class(cp) == wchartype) { - int w = wcwidth(*(uint32_t*)cp_data(cp)); + int w = utf8proc_charwidth(*(uint32_t*)cp_data(cp)); if (w < 0) return FL_F; return fixnum(w); diff --git a/src/julia.expmap b/src/julia.expmap index 04faf65a0e7bf7..f7a703552bdcd9 100644 --- a/src/julia.expmap +++ b/src/julia.expmap @@ -29,7 +29,6 @@ rl_clear_input; save_arg_area_loc; u8_*; - wcwidth; uv_*; add_library_mapping; utf8proc_*; diff --git a/src/support/Makefile b/src/support/Makefile index f0e605c94fe84d..6538c35d921c5e 100644 --- a/src/support/Makefile +++ b/src/support/Makefile @@ -11,7 +11,7 @@ SRCS = hashing timefuncs ptrhash operators \ utf8 ios htable bitvector \ int2str libsupportinit arraylist strtod ifeq ($(OS),WINNT) -SRCS += asprintf wcwidth +SRCS += asprintf ifeq ($(ARCH),i686) SRCS += _setjmp.win32 _longjmp.win32 else ifeq ($(ARCH),i386) @@ -26,7 +26,7 @@ HEADERS = $(wildcard *.h) $(LIBUV_INC)/uv.h OBJS = $(SRCS:%=$(BUILDDIR)/%.o) DOBJS = $(SRCS:%=$(BUILDDIR)/%.dbg.obj) -FLAGS = $(CFLAGS) $(HFILEDIRS:%=-I%) -I$(LIBUV_INC) -DLIBRARY_EXPORTS +FLAGS = $(CFLAGS) $(HFILEDIRS:%=-I%) -I$(LIBUV_INC) -I$(UTF8PROC_INC) -DLIBRARY_EXPORTS -DUTF8PROC_EXPORT ifneq ($(USEMSVC), 1) FLAGS += -Wall -Wno-strict-aliasing -fvisibility=hidden endif diff --git a/src/support/Windows.mk b/src/support/Windows.mk index 2c7909c6e11917..7fc2dd42c50f0b 100644 --- a/src/support/Windows.mk +++ b/src/support/Windows.mk @@ -34,8 +34,7 @@ OBJECTS = \ int2str.obj \ libsupportinit.obj \ arraylist.obj \ - asprintf.obj \ - wcwidth.obj + asprintf.obj INCLUDE = $(INCLUDE);$(MAKEDIR)\..\..\deps\libuv\include CFLAGS = $(CFLAGS) -D_CRT_SECURE_NO_WARNINGS -DLIBRARY_EXPORTS diff --git a/src/support/utf8.c b/src/support/utf8.c index 2533248eb33e36..e9fb38ecb088d5 100644 --- a/src/support/utf8.c +++ b/src/support/utf8.c @@ -34,6 +34,9 @@ #include "utf8.h" +#undef DLLEXPORT /* avoid conflicting definition */ +#include "utf8proc.h" + #ifdef __cplusplus extern "C" { #endif @@ -261,17 +264,10 @@ size_t u8_strlen(const char *s) return count; } -#if defined(_OS_WINDOWS_) -extern int wcwidth(uint32_t ch); -#elif defined(_OS_LINUX_) -extern int wcwidth(wchar_t ch); -#endif - size_t u8_strwidth(const char *s) { uint32_t ch; size_t nb, tot=0; - int w; signed char sc; while ((sc = (signed char)*s) != 0) { @@ -293,8 +289,7 @@ size_t u8_strwidth(const char *s) case 0: ch += (unsigned char)*s++; } ch -= offsetsFromUTF8[nb]; - w = wcwidth(ch); // might return -1 - if (w > 0) tot += w; + tot += utf8proc_charwidth(ch); } } return tot; diff --git a/src/support/wcwidth.c b/src/support/wcwidth.c deleted file mode 100644 index c74a28967f10ee..00000000000000 --- a/src/support/wcwidth.c +++ /dev/null @@ -1,323 +0,0 @@ -#include "dtypes.h" //for DLLEXPORT -/* - * This is an implementation of wcwidth() and wcswidth() (defined in - * IEEE Std 1002.1-2001) for Unicode. - * - * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html - * http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html - * - * In fixed-width output devices, Latin characters all occupy a single - * "cell" position of equal width, whereas ideographic CJK characters - * occupy two such cells. Interoperability between terminal-line - * applications and (teletype-style) character terminals using the - * UTF-8 encoding requires agreement on which character should advance - * the cursor by how many cell positions. No established formal - * standards exist at present on which Unicode character shall occupy - * how many cell positions on character terminals. These routines are - * a first attempt of defining such behavior based on simple rules - * applied to data provided by the Unicode Consortium. - * - * For some graphical characters, the Unicode standard explicitly - * defines a character-cell width via the definition of the East Asian - * FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes. - * In all these cases, there is no ambiguity about which width a - * terminal shall use. For characters in the East Asian Ambiguous (A) - * class, the width choice depends purely on a preference of backward - * compatibility with either historic CJK or Western practice. - * Choosing single-width for these characters is easy to justify as - * the appropriate long-term solution, as the CJK practice of - * displaying these characters as double-width comes from historic - * implementation simplicity (8-bit encoded characters were displayed - * single-width and 16-bit ones double-width, even for Greek, - * Cyrillic, etc.) and not any typographic considerations. - * - * Much less clear is the choice of width for the Not East Asian - * (Neutral) class. Existing practice does not dictate a width for any - * of these characters. It would nevertheless make sense - * typographically to allocate two character cells to characters such - * as for instance EM SPACE or VOLUME INTEGRAL, which cannot be - * represented adequately with a single-width glyph. The following - * routines at present merely assign a single-cell width to all - * neutral characters, in the interest of simplicity. This is not - * entirely satisfactory and should be reconsidered before - * establishing a formal standard in this area. At the moment, the - * decision which Not East Asian (Neutral) characters should be - * represented by double-width glyphs cannot yet be answered by - * applying a simple rule from the Unicode database content. Setting - * up a proper standard for the behavior of UTF-8 character terminals - * will require a careful analysis not only of each Unicode character, - * but also of each presentation form, something the author of these - * routines has avoided to do so far. - * - * http://www.unicode.org/unicode/reports/tr11/ - * - * Markus Kuhn -- 2007-05-26 (Unicode 5.0) - * - * Permission to use, copy, modify, and distribute this software - * for any purpose and without fee is hereby granted. The author - * disclaims all warranties with regard to this software. - * - * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c - * - * MODIFIED TO USE uint32_t - */ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct interval { - int first; - int last; -}; - -/* auxiliary function for binary search in interval table */ -static int bisearch(uint32_t ucs, const struct interval *table, int max) { - int min = 0; - int mid; - - if (ucs < table[0].first || ucs > table[max].last) - return 0; - while (max >= min) { - mid = (min + max) / 2; - if (ucs > table[mid].last) - min = mid + 1; - else if (ucs < table[mid].first) - max = mid - 1; - else - return 1; - } - - return 0; -} - - -/* The following two functions define the column width of an ISO 10646 - * character as follows: - * - * - The null character (U+0000) has a column width of 0. - * - * - Other C0/C1 control characters and DEL will lead to a return - * value of -1. - * - * - Non-spacing and enclosing combining characters (general - * category code Mn or Me in the Unicode database) have a - * column width of 0. - * - * - SOFT HYPHEN (U+00AD) has a column width of 1. - * - * - Other format characters (general category code Cf in the Unicode - * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0. - * - * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF) - * have a column width of 0. - * - * - Spacing characters in the East Asian Wide (W) or East Asian - * Full-width (F) category as defined in Unicode Technical - * Report #11 have a column width of 2. - * - * - All remaining characters (including all printable - * ISO 8859-1 and WGL4 characters, Unicode control characters, - * etc.) have a column width of 1. - * - * This implementation assumes that wchar_t characters are encoded - * in ISO 10646. - */ - -#include -#include - -DLLEXPORT int wcwidth(uint32_t ucs) -{ - /* sorted list of non-overlapping intervals of non-spacing characters */ - /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */ - static const struct interval combining[] = { - { 0x0300, 0x036F }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 }, - { 0x0591, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 }, - { 0x05C4, 0x05C5 }, { 0x05C7, 0x05C7 }, { 0x0600, 0x0603 }, - { 0x0610, 0x0615 }, { 0x064B, 0x065E }, { 0x0670, 0x0670 }, - { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED }, - { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A }, - { 0x07A6, 0x07B0 }, { 0x07EB, 0x07F3 }, { 0x0901, 0x0902 }, - { 0x093C, 0x093C }, { 0x0941, 0x0948 }, { 0x094D, 0x094D }, - { 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 }, - { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD }, - { 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 }, { 0x0A3C, 0x0A3C }, - { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D }, - { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC }, - { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD }, - { 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C }, - { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D }, - { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 }, - { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 }, - { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBC, 0x0CBC }, - { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD }, - { 0x0CE2, 0x0CE3 }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D }, - { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 }, - { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E }, - { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC }, - { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 }, - { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E }, - { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 }, - { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 }, - { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 }, - { 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x135F, 0x135F }, - { 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 }, - { 0x1772, 0x1773 }, { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD }, - { 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD }, - { 0x180B, 0x180D }, { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 }, - { 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193B }, - { 0x1A17, 0x1A18 }, { 0x1B00, 0x1B03 }, { 0x1B34, 0x1B34 }, - { 0x1B36, 0x1B3A }, { 0x1B3C, 0x1B3C }, { 0x1B42, 0x1B42 }, - { 0x1B6B, 0x1B73 }, { 0x1DC0, 0x1DCA }, { 0x1DFE, 0x1DFF }, - { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x2060, 0x2063 }, - { 0x206A, 0x206F }, { 0x20D0, 0x20EF }, { 0x302A, 0x302F }, - { 0x3099, 0x309A }, { 0xA806, 0xA806 }, { 0xA80B, 0xA80B }, - { 0xA825, 0xA826 }, { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F }, - { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB }, - { 0x10A01, 0x10A03 }, { 0x10A05, 0x10A06 }, { 0x10A0C, 0x10A0F }, - { 0x10A38, 0x10A3A }, { 0x10A3F, 0x10A3F }, { 0x1D167, 0x1D169 }, - { 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B }, { 0x1D1AA, 0x1D1AD }, - { 0x1D242, 0x1D244 }, { 0xE0001, 0xE0001 }, { 0xE0020, 0xE007F }, - { 0xE0100, 0xE01EF } - }; - - /* test for 8-bit control characters */ - if (ucs == 0) - return 0; - if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0)) - return -1; - - /* binary search in table of non-spacing characters */ - if (bisearch(ucs, combining, - sizeof(combining) / sizeof(struct interval) - 1)) - return 0; - - /* if we arrive here, ucs is not a combining or C0/C1 control character */ - - return 1 + - (ucs >= 0x1100 && - (ucs <= 0x115f || /* Hangul Jamo init. consonants */ - ucs == 0x2329 || ucs == 0x232a || - (ucs >= 0x2e80 && ucs <= 0xa4cf && - ucs != 0x303f) || /* CJK ... Yi */ - (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */ - (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */ - (ucs >= 0xfe10 && ucs <= 0xfe19) || /* Vertical forms */ - (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */ - (ucs >= 0xff00 && ucs <= 0xff60) || /* Fullwidth Forms */ - (ucs >= 0xffe0 && ucs <= 0xffe6) || - (ucs >= 0x20000 && ucs <= 0x2fffd) || - (ucs >= 0x30000 && ucs <= 0x3fffd))); -} - - -int wcswidth(const uint32_t *pwcs, size_t n) -{ - int w, width = 0; - - for (;*pwcs && n-- > 0; pwcs++) - if ((w = wcwidth(*pwcs)) < 0) - return -1; - else - width += w; - - return width; -} - - -/* - * The following functions are the same as wcwidth() and - * wcswidth(), except that spacing characters in the East Asian - * Ambiguous (A) category as defined in Unicode Technical Report #11 - * have a column width of 2. This variant might be useful for users of - * CJK legacy encodings who want to migrate to UCS without changing - * the traditional terminal character-width behaviour. It is not - * otherwise recommended for general use. - */ -int wcwidth_cjk(uint32_t ucs) -{ - /* sorted list of non-overlapping intervals of East Asian Ambiguous - * characters, generated by "uniset +WIDTH-A -cat=Me -cat=Mn -cat=Cf c" */ - static const struct interval ambiguous[] = { - { 0x00A1, 0x00A1 }, { 0x00A4, 0x00A4 }, { 0x00A7, 0x00A8 }, - { 0x00AA, 0x00AA }, { 0x00AE, 0x00AE }, { 0x00B0, 0x00B4 }, - { 0x00B6, 0x00BA }, { 0x00BC, 0x00BF }, { 0x00C6, 0x00C6 }, - { 0x00D0, 0x00D0 }, { 0x00D7, 0x00D8 }, { 0x00DE, 0x00E1 }, - { 0x00E6, 0x00E6 }, { 0x00E8, 0x00EA }, { 0x00EC, 0x00ED }, - { 0x00F0, 0x00F0 }, { 0x00F2, 0x00F3 }, { 0x00F7, 0x00FA }, - { 0x00FC, 0x00FC }, { 0x00FE, 0x00FE }, { 0x0101, 0x0101 }, - { 0x0111, 0x0111 }, { 0x0113, 0x0113 }, { 0x011B, 0x011B }, - { 0x0126, 0x0127 }, { 0x012B, 0x012B }, { 0x0131, 0x0133 }, - { 0x0138, 0x0138 }, { 0x013F, 0x0142 }, { 0x0144, 0x0144 }, - { 0x0148, 0x014B }, { 0x014D, 0x014D }, { 0x0152, 0x0153 }, - { 0x0166, 0x0167 }, { 0x016B, 0x016B }, { 0x01CE, 0x01CE }, - { 0x01D0, 0x01D0 }, { 0x01D2, 0x01D2 }, { 0x01D4, 0x01D4 }, - { 0x01D6, 0x01D6 }, { 0x01D8, 0x01D8 }, { 0x01DA, 0x01DA }, - { 0x01DC, 0x01DC }, { 0x0251, 0x0251 }, { 0x0261, 0x0261 }, - { 0x02C4, 0x02C4 }, { 0x02C7, 0x02C7 }, { 0x02C9, 0x02CB }, - { 0x02CD, 0x02CD }, { 0x02D0, 0x02D0 }, { 0x02D8, 0x02DB }, - { 0x02DD, 0x02DD }, { 0x02DF, 0x02DF }, { 0x0391, 0x03A1 }, - { 0x03A3, 0x03A9 }, { 0x03B1, 0x03C1 }, { 0x03C3, 0x03C9 }, - { 0x0401, 0x0401 }, { 0x0410, 0x044F }, { 0x0451, 0x0451 }, - { 0x2010, 0x2010 }, { 0x2013, 0x2016 }, { 0x2018, 0x2019 }, - { 0x201C, 0x201D }, { 0x2020, 0x2022 }, { 0x2024, 0x2027 }, - { 0x2030, 0x2030 }, { 0x2032, 0x2033 }, { 0x2035, 0x2035 }, - { 0x203B, 0x203B }, { 0x203E, 0x203E }, { 0x2074, 0x2074 }, - { 0x207F, 0x207F }, { 0x2081, 0x2084 }, { 0x20AC, 0x20AC }, - { 0x2103, 0x2103 }, { 0x2105, 0x2105 }, { 0x2109, 0x2109 }, - { 0x2113, 0x2113 }, { 0x2116, 0x2116 }, { 0x2121, 0x2122 }, - { 0x2126, 0x2126 }, { 0x212B, 0x212B }, { 0x2153, 0x2154 }, - { 0x215B, 0x215E }, { 0x2160, 0x216B }, { 0x2170, 0x2179 }, - { 0x2190, 0x2199 }, { 0x21B8, 0x21B9 }, { 0x21D2, 0x21D2 }, - { 0x21D4, 0x21D4 }, { 0x21E7, 0x21E7 }, { 0x2200, 0x2200 }, - { 0x2202, 0x2203 }, { 0x2207, 0x2208 }, { 0x220B, 0x220B }, - { 0x220F, 0x220F }, { 0x2211, 0x2211 }, { 0x2215, 0x2215 }, - { 0x221A, 0x221A }, { 0x221D, 0x2220 }, { 0x2223, 0x2223 }, - { 0x2225, 0x2225 }, { 0x2227, 0x222C }, { 0x222E, 0x222E }, - { 0x2234, 0x2237 }, { 0x223C, 0x223D }, { 0x2248, 0x2248 }, - { 0x224C, 0x224C }, { 0x2252, 0x2252 }, { 0x2260, 0x2261 }, - { 0x2264, 0x2267 }, { 0x226A, 0x226B }, { 0x226E, 0x226F }, - { 0x2282, 0x2283 }, { 0x2286, 0x2287 }, { 0x2295, 0x2295 }, - { 0x2299, 0x2299 }, { 0x22A5, 0x22A5 }, { 0x22BF, 0x22BF }, - { 0x2312, 0x2312 }, { 0x2460, 0x24E9 }, { 0x24EB, 0x254B }, - { 0x2550, 0x2573 }, { 0x2580, 0x258F }, { 0x2592, 0x2595 }, - { 0x25A0, 0x25A1 }, { 0x25A3, 0x25A9 }, { 0x25B2, 0x25B3 }, - { 0x25B6, 0x25B7 }, { 0x25BC, 0x25BD }, { 0x25C0, 0x25C1 }, - { 0x25C6, 0x25C8 }, { 0x25CB, 0x25CB }, { 0x25CE, 0x25D1 }, - { 0x25E2, 0x25E5 }, { 0x25EF, 0x25EF }, { 0x2605, 0x2606 }, - { 0x2609, 0x2609 }, { 0x260E, 0x260F }, { 0x2614, 0x2615 }, - { 0x261C, 0x261C }, { 0x261E, 0x261E }, { 0x2640, 0x2640 }, - { 0x2642, 0x2642 }, { 0x2660, 0x2661 }, { 0x2663, 0x2665 }, - { 0x2667, 0x266A }, { 0x266C, 0x266D }, { 0x266F, 0x266F }, - { 0x273D, 0x273D }, { 0x2776, 0x277F }, { 0xE000, 0xF8FF }, - { 0xFFFD, 0xFFFD }, { 0xF0000, 0xFFFFD }, { 0x100000, 0x10FFFD } - }; - - /* binary search in table of non-spacing characters */ - if (bisearch(ucs, ambiguous, - sizeof(ambiguous) / sizeof(struct interval) - 1)) - return 2; - - return wcwidth(ucs); -} - - -int wcswidth_cjk(const uint32_t *pwcs, size_t n) -{ - int w, width = 0; - - for (;*pwcs && n-- > 0; pwcs++) - if ((w = wcwidth_cjk(*pwcs)) < 0) - return -1; - else - width += w; - - return width; -} - -#ifdef __cplusplus -} -#endif diff --git a/test/unicode.jl b/test/unicode.jl index a1326f422c58dc..c0342b46d1caca 100644 --- a/test/unicode.jl +++ b/test/unicode.jl @@ -125,3 +125,6 @@ let grphtest = (("b\u0300lahβlahb\u0302láh", ["b\u0300","l","a","h", end end end + +# up-to-date character widths (#3721, #6939) +@test charwidth('\U1f355') == strwidth("\U1f355") == strwidth(utf16("\U1f355")) == strwidth("\U1f355\u0302") == strwidth(utf16("\U1f355\u0302")) == 2