Skip to content

Commit

Permalink
add runewidth
Browse files Browse the repository at this point in the history
  • Loading branch information
cls committed Mar 13, 2015
1 parent 4310117 commit 89d4832
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SRC = $(GEN) \
src/runestrstr.c \
src/runetochar.c \
src/runetype.c \
src/runewidth.c \
src/utfecpy.c \
src/utflen.c \
src/utfnlen.c \
Expand Down
1 change: 1 addition & 0 deletions include/utf.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Rune *runestrrchr(const Rune *, Rune);
Rune *runestrdup(const Rune *);
Rune *runestrstr(const Rune *, const Rune *);

int runewidth(Rune);
int utftorunestr(Rune *, const char *);

#ifdef __cplusplus
Expand Down
29 changes: 29 additions & 0 deletions src/runewidth.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* See LICENSE file for copyright and license details. */
#include "utf.h"

int
runewidth(Rune r)
{
/* Adapted from Markus Kuhn's wcwidth() */

if(r == 0)
return 0;
else if(iscntrlrune(r))
return -1;
else if(iscombiningrune(r))
return 0;
else if((r >= 0x1100 && r <= 0x115F)
|| (r >= 0x2329 && r <= 0x232A)
|| (r >= 0x2E80 && r <= 0xA4CF && r != 0x303F)
|| (r >= 0xAC00 && r <= 0xD7A3)
|| (r >= 0xF900 && r <= 0xFAFF)
|| (r >= 0xFE10 && r <= 0xFE19)
|| (r >= 0xFE30 && r <= 0xFE6F)
|| (r >= 0xFF00 && r <= 0xFF60)
|| (r >= 0xFFE0 && r <= 0xFFE6)
|| (r >= RUNE_C(0x20000) && r <= RUNE_C(0x2FFFD))
|| (r >= RUNE_C(0x30000) && r <= RUNE_C(0x3FFFD)))
return 2;
else
return 1;
}

0 comments on commit 89d4832

Please sign in to comment.