From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Michal Nazarewicz Newsgroups: gmane.emacs.bugs Subject: bug#24603: [PATCHv5 04/11] Split up casify_region function (bug#24603) Date: Thu, 9 Mar 2017 22:51:43 +0100 Message-ID: <20170309215150.9562-5-mina86@mina86.com> References: <20170309215150.9562-1-mina86@mina86.com> NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1489096438 10379 195.159.176.226 (9 Mar 2017 21:53:58 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 9 Mar 2017 21:53:58 +0000 (UTC) To: 24603@debbugs.gnu.org, eliz@gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Thu Mar 09 22:53:49 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cm60B-0001IJ-R9 for geb-bug-gnu-emacs@m.gmane.org; Thu, 09 Mar 2017 22:53:40 +0100 Original-Received: from localhost ([::1]:36406 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cm60H-0003Bp-QY for geb-bug-gnu-emacs@m.gmane.org; Thu, 09 Mar 2017 16:53:45 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41173) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cm5zd-0002g7-Td for bug-gnu-emacs@gnu.org; Thu, 09 Mar 2017 16:53:09 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cm5zb-0004Is-04 for bug-gnu-emacs@gnu.org; Thu, 09 Mar 2017 16:53:05 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:49928) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cm5za-0004Io-T6 for bug-gnu-emacs@gnu.org; Thu, 09 Mar 2017 16:53:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cm5za-0000P4-Mp for bug-gnu-emacs@gnu.org; Thu, 09 Mar 2017 16:53:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Michal Nazarewicz Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 09 Mar 2017 21:53:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 24603 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 24603-submit@debbugs.gnu.org id=B24603.14890963291351 (code B ref 24603); Thu, 09 Mar 2017 21:53:02 +0000 Original-Received: (at 24603) by debbugs.gnu.org; 9 Mar 2017 21:52:09 +0000 Original-Received: from localhost ([127.0.0.1]:48097 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cm5yi-0000Ld-G8 for submit@debbugs.gnu.org; Thu, 09 Mar 2017 16:52:08 -0500 Original-Received: from mail-wr0-f181.google.com ([209.85.128.181]:36793) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cm5yg-0000Kq-Rj for 24603@debbugs.gnu.org; Thu, 09 Mar 2017 16:52:07 -0500 Original-Received: by mail-wr0-f181.google.com with SMTP id u108so53785026wrb.3 for <24603@debbugs.gnu.org>; Thu, 09 Mar 2017 13:52:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=nqUJ5ysUWFZDDw70xoT7rj4Ab0KlkqpZt0wSzy6ofCY=; b=FBX/gaFvWV8TA6R3w1w0y6reCpMr9qSrz0RsbgRts1pn25xfnIMXzUTy5Ci85aX1dZ IDcfvR09JHlgCLmJb25Lz1qehd9MATthEr093RI+fNLuLS9zkbk5tHEBA6iRFnJSE0xh GRadwt2W90Qkmx7Ekd5veN4tm4dWwSvvunTfGKpwCtZ7qfnnBwT1j3xIJ8zEu7O2sJFA x9IiatBqh8XNpEkbrHhxy2bg5iGwOPLrujPi7mp7QZvijyxgv00aeA9jntleOLjbo5pV VVQCfYuWCvWs1aRhSIzqvHNXo66YJ0ZwCoScAl1O0ql7y9/iFciUwtePF9NNe965H+IU 0nEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references; bh=nqUJ5ysUWFZDDw70xoT7rj4Ab0KlkqpZt0wSzy6ofCY=; b=kmMxNQ7+EXEx1AfcxvWwrJTU23xuetU5k5gIJKg01Xgs+N810G1KVYqKTcDUV+Tg6h o1Cqm+3eD7fTiIyKdjhy4mkVYFBAsJH2UdVpTngWsqD7bYX7tV9vVpA8FfK0E+AwnSM5 GQYn97YAkWgNR62Np3FuhcJS1oucIMPcrol/IGgG5pqf75voxjxZBMn30q7PWfsMykMm FRMIdfvGCagrPtgBrk8uIgZp6Qoo8fZM0X7Rglp/cE/vdF0ULUno4U1WVXw+rKjLARRJ UxYJzrz7TuQU16ubcxjk88Bx5Yw+JyKg25vKVkpMrDyDi8G04Tv3s14AMCtz8+oL9pjy ToLA== X-Gm-Message-State: AMke39lblTs0fhcEdpcQcLAU0ciHRj0nuRVaHe9UazWzXctoXY7EMUEuugUPox8RrhALzJhV X-Received: by 10.223.139.83 with SMTP id v19mr12150469wra.142.1489096320675; Thu, 09 Mar 2017 13:52:00 -0800 (PST) Original-Received: from mpn.zrh.corp.google.com ([2620:0:105f:303:6127:4508:f312:7f95]) by smtp.gmail.com with ESMTPSA id m83sm244484wmc.33.2017.03.09.13.51.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Mar 2017 13:51:59 -0800 (PST) Original-Received: by mpn.zrh.corp.google.com (Postfix, from userid 126942) id 4E37D1E01F7; Thu, 9 Mar 2017 22:51:58 +0100 (CET) X-Mailer: git-send-email 2.12.0.246.ga2ecc84866-goog In-Reply-To: <20170309215150.9562-1-mina86@mina86.com> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:130399 Archived-At: No functional changes at this time but splitting casify_region into a function dealing with multibyte and another dealing with unibyte buffers will make future code changes slightly easier. * src/casefiddle.c (casify_region): Move most of the code into two new functions: (do_casify_multibyte_region, do_casify_unibyte_region): new functions. --- src/casefiddle.c | 159 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 86 insertions(+), 73 deletions(-) diff --git a/src/casefiddle.c b/src/casefiddle.c index 01e35194e0e..88da609b746 100644 --- a/src/casefiddle.c +++ b/src/casefiddle.c @@ -234,102 +234,115 @@ The argument object is not altered--the value is a copy. */) return casify_object (CASE_CAPITALIZE_UP, obj); } -/* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP. - b and e specify range of buffer to operate on. */ - -static void -casify_region (enum case_action flag, Lisp_Object b, Lisp_Object e) +/* Based on CTX, case region in a unibyte buffer from POS to *ENDP. Return + first position that has changed and save last position in *ENDP. If no + characters were changed, return -1 and *ENDP is unspecified. */ +static ptrdiff_t +do_casify_unibyte_region (struct casing_context *ctx, + ptrdiff_t pos, ptrdiff_t *endp) { - bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters)); - ptrdiff_t start, end; - ptrdiff_t start_byte; + ptrdiff_t first = -1, last = -1; /* Position of first and last changes. */ + ptrdiff_t end = *endp; + int ch, cased; - /* Position of first and last changes. */ - ptrdiff_t first = -1, last; + for (; pos < end; ++pos) + { + ch = FETCH_BYTE (pos); + MAKE_CHAR_MULTIBYTE (ch); - ptrdiff_t opoint = PT; - ptrdiff_t opoint_byte = PT_BYTE; + cased = case_character (ctx, ch); + if (cased == ch) + continue; - struct casing_context ctx; + last = pos; + if (first < 0) + first = pos; - if (EQ (b, e)) - /* Not modifying because nothing marked */ - return; + MAKE_CHAR_UNIBYTE (cased); + FETCH_BYTE (pos) = cased; + } - validate_region (&b, &e); - start = XFASTINT (b); - end = XFASTINT (e); - modify_text (start, end); - record_change (start, end - start); - start_byte = CHAR_TO_BYTE (start); + *endp = last + 1; + return first; +} - prepare_casing_context (&ctx, flag, true); +/* Based on CTX, case region in a multibyte buffer from POS to *ENDP. Return + first position that has changed and save last position in *ENDP. If no + characters were changed, return -1 and *ENDP is unspecified. */ +static ptrdiff_t +do_casify_multibyte_region (struct casing_context *ctx, + ptrdiff_t pos, ptrdiff_t *endp) +{ + ptrdiff_t first = -1, last = -1; /* Position of first and last changes. */ + ptrdiff_t pos_byte = CHAR_TO_BYTE (pos), end = *endp; + ptrdiff_t opoint = PT; + int ch, cased, len; - while (start < end) + while (pos < end) { - int ch, cased, len; - - if (multibyte) - { - ch = FETCH_MULTIBYTE_CHAR (start_byte); - len = CHAR_BYTES (ch); - } - else - { - ch = FETCH_BYTE (start_byte); - MAKE_CHAR_MULTIBYTE (ch); - len = 1; - } - cased = case_character (&ctx, ch); - if (ch != cased) + ch = STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (pos_byte), len); + cased = case_character (ctx, ch); + if (cased != ch) { - last = start; + last = pos; if (first < 0) - first = start; + first = pos; - if (! multibyte) - { - MAKE_CHAR_UNIBYTE (cased); - FETCH_BYTE (start_byte) = cased; - } - else if (ASCII_CHAR_P (cased) && ASCII_CHAR_P (ch)) - FETCH_BYTE (start_byte) = cased; + if (ASCII_CHAR_P (cased) && ASCII_CHAR_P (ch)) + FETCH_BYTE (pos_byte) = cased; else { - int tolen = CHAR_BYTES (cased); - int j; unsigned char str[MAX_MULTIBYTE_LENGTH]; - - CHAR_STRING (cased, str); - if (len == tolen) - { - /* Length is unchanged. */ - for (j = 0; j < len; ++j) - FETCH_BYTE (start_byte + j) = str[j]; - } + int totlen = CHAR_STRING (cased, str); + if (len == totlen) + memcpy (BYTE_POS_ADDR (pos_byte), str, len); else - { - /* Replace one character with the other, - keeping text properties the same. */ - replace_range_2 (start, start_byte, - start + 1, start_byte + len, - (char *) str, 1, tolen, - 0); - len = tolen; - } + /* Replace one character with the other(s), keeping text + properties the same. */ + replace_range_2 (pos, pos_byte, pos + 1, pos_byte + len, + (char *) str, 9, totlen, 0); + len = totlen; } } - start++; - start_byte += len; + pos++; + pos_byte += len; } if (PT != opoint) - TEMP_SET_PT_BOTH (opoint, opoint_byte); + TEMP_SET_PT_BOTH (opoint, CHAR_TO_BYTE (opoint)); + + *endp = last; + return first; +} + +/* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP. + b and e specify range of buffer to operate on. */ +static void +casify_region (enum case_action flag, Lisp_Object b, Lisp_Object e) +{ + struct casing_context ctx; + ptrdiff_t start, end; + + if (EQ (b, e)) + /* Not modifying because nothing marked */ + return; + + validate_region (&b, &e); + start = XFASTINT (b); + end = XFASTINT (e); + modify_text (start, end); + record_change (start, end - start); + prepare_casing_context (&ctx, flag, true); + + if (NILP (BVAR (current_buffer, enable_multibyte_characters))) + start = do_casify_unibyte_region (&ctx, start, &end); + else + start = do_casify_multibyte_region (&ctx, start, &end); - if (first >= 0) + if (start >= 0) { - signal_after_change (first, last + 1 - first, last + 1 - first); - update_compositions (first, last + 1, CHECK_ALL); + signal_after_change (start, end + 1 - start, end + 1 - start); + update_compositions (start, end + 1, CHECK_ALL); } } -- 2.12.0.246.ga2ecc84866-goog