Commits

Anonymous committed 7e4f67a

correct wide character support

Comments (0)

Files changed (1)

offsets/offsets.c

  * combine with offsets.awk to get a listing of word [offset,...] pairs
  */
 
-#include <ctype.h>
+#include <locale.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <wchar.h>
+#include <wctype.h>
 
 int main(void)
 {
 	size_t off;
-	char c, buf[256], *p = buf;
+	wint_t c;
+	wchar_t buf[256], *p = buf;
+	char mbs[MB_CUR_MAX];
 
-	for (off = 0; (c = getchar()) != EOF; off++) {
-		if (isalnum(c)) {
+	setlocale(LC_ALL,"");
+
+	for (off = 0; (c = fgetwc(stdin)) != WEOF; off += wctomb(mbs, c)) {
+		if (iswalnum(c)) {
 			*p++ = c;
 		} else if (p != buf) {
-			*p = '\0';
-			printf("%s %zu\n", buf, off - (p - buf));
+			*p = 0;
+			printf("%ls %zu\n", buf, off - wcstombs(NULL, buf, 0));
 			p = buf;
 		}
 	}