From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Subject: [PATCH 5/9] utf8.c: add utf8_strnwidth() with the ability to skip ansi sequences Date: Sun, 23 Sep 2012 16:10:29 +0700 Message-ID: <1348391433-11300-6-git-send-email-pclouds@gmail.com> References: <1348287739-12128-1-git-send-email-pclouds@gmail.com> <1348391433-11300-1-git-send-email-pclouds@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Junio C Hamano , Jeff King , =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Sun Sep 23 11:18:01 2012 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TFiK4-0001QJ-CD for gcvg-git-2@plane.gmane.org; Sun, 23 Sep 2012 11:17:56 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753577Ab2IWJRs convert rfc822-to-quoted-printable (ORCPT ); Sun, 23 Sep 2012 05:17:48 -0400 Received: from mail-pb0-f46.google.com ([209.85.160.46]:35801 "EHLO mail-pb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753551Ab2IWJRr (ORCPT ); Sun, 23 Sep 2012 05:17:47 -0400 Received: by mail-pb0-f46.google.com with SMTP id rr4so5429111pbb.19 for ; Sun, 23 Sep 2012 02:17:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=GMNXYsDRP5Knoi5re9pAX30lKmEzfjL9MykBRyuye/o=; b=PNrAM1/eqIIKhhEO2y8o1jdFZxjKiFIuSBgwq0S53WVJSL9+3PN7G8NGAhRoUamEOC 12Zhq6yCZ0o+upRPr7rxBq/NH2DSw7/DAJwO4eoZhbB+UMuSVHtPL52qOLppKaMLqXY9 weXP9oYvFhiZ4ovCIpnF1LaWyMXAP6kqHr2pgIcdgO30lWaaFbl84bKtS9kvMstYbSnB +4L1JbT41ePgBe2htEsJiezSBVdq1mCFN40DIOfNRCLGHouUPay35RAkE4etH3BKDcQA zAraMi3Km9GEXO8dfy3UW4berri8D8x27APEG6pmfveoBOYKL1L2khfFqoBRaz7IV64K hmIQ== Received: by 10.68.226.167 with SMTP id rt7mr28093643pbc.146.1348391866847; Sun, 23 Sep 2012 02:17:46 -0700 (PDT) Received: from pclouds@gmail.com ([115.74.47.110]) by mx.google.com with ESMTPS id kc9sm7855027pbb.11.2012.09.23.02.17.43 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 23 Sep 2012 02:17:46 -0700 (PDT) Received: by pclouds@gmail.com (sSMTP sendmail emulation); Sun, 23 Sep 2012 16:11:04 +0700 X-Mailer: git-send-email 1.7.12.1.406.g6ab07c4 In-Reply-To: <1348391433-11300-1-git-send-email-pclouds@gmail.com> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: Signed-off-by: Nguy=E1=BB=85n Th=C3=A1i Ng=E1=BB=8Dc Duy --- utf8.c | 20 ++++++++++++++------ utf8.h | 1 + 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/utf8.c b/utf8.c index 18a4d9c..791499e 100644 --- a/utf8.c +++ b/utf8.c @@ -266,18 +266,26 @@ int utf8_width(const char **start, size_t *remain= der_p) * string, assuming that the string is utf8. Returns strlen() instead * if the string does not look like a valid utf8 string. */ -int utf8_strwidth(const char *string) +int utf8_strnwidth(const char *string, int len, int skip_ansi) { int width =3D 0; const char *orig =3D string; =20 - while (1) { - if (!string) - return strlen(orig); - if (!*string) - return width; + if (len =3D=3D -1) + len =3D strlen(string); + while (string && string < orig + len) { + int skip; + while (skip_ansi && + (skip =3D display_mode_esc_sequence_len(string))) + string +=3D skip; width +=3D utf8_width(&string, NULL); } + return string ? width : len; +} + +int utf8_strwidth(const char *string) +{ + return utf8_strnwidth(string, -1, 0); } =20 int is_utf8(const char *text) diff --git a/utf8.h b/utf8.h index 3c0ae76..58d5b6b 100644 --- a/utf8.h +++ b/utf8.h @@ -4,6 +4,7 @@ typedef unsigned int ucs_char_t; /* assuming 32bit int */ =20 int utf8_width(const char **start, size_t *remainder_p); +int utf8_strnwidth(const char *string, int len, int skip_ansi); int utf8_strwidth(const char *string); int is_utf8(const char *text); int is_encoding_utf8(const char *name); --=20 1.7.12.1.406.g6ab07c4