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: [PATCHv6 4/6] Split up casify_region function (bug#24603) Date: Tue, 21 Mar 2017 02:27:07 +0100 Message-ID: <20170321012709.19402-5-mina86@mina86.com> References: <20170309215150.9562-1-mina86@mina86.com> <20170321012709.19402-1-mina86@mina86.com> NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1490059734 20994 195.159.176.226 (21 Mar 2017 01:28:54 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 21 Mar 2017 01:28:54 +0000 (UTC) To: Eli Zaretskii , Andreas Schwab , 24603@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Mar 21 02:28:50 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 1cq8bO-0004hM-0Y for geb-bug-gnu-emacs@m.gmane.org; Tue, 21 Mar 2017 02:28:46 +0100 Original-Received: from localhost ([::1]:36108 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cq8bT-00071o-Vl for geb-bug-gnu-emacs@m.gmane.org; Mon, 20 Mar 2017 21:28:52 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:46894) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cq8am-0006Ww-9J for bug-gnu-emacs@gnu.org; Mon, 20 Mar 2017 21:28:09 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cq8aj-0006qQ-3n for bug-gnu-emacs@gnu.org; Mon, 20 Mar 2017 21:28:08 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:38817) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cq8ai-0006qG-W1 for bug-gnu-emacs@gnu.org; Mon, 20 Mar 2017 21:28:05 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cq8ai-0008P7-Pe for bug-gnu-emacs@gnu.org; Mon, 20 Mar 2017 21:28:04 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Michal Nazarewicz Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 21 Mar 2017 01:28:04 +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.149005965632224 (code B ref 24603); Tue, 21 Mar 2017 01:28:04 +0000 Original-Received: (at 24603) by debbugs.gnu.org; 21 Mar 2017 01:27:36 +0000 Original-Received: from localhost ([127.0.0.1]:37009 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cq8aG-0008NZ-Cd for submit@debbugs.gnu.org; Mon, 20 Mar 2017 21:27:36 -0400 Original-Received: from mail-wm0-f41.google.com ([74.125.82.41]:36504) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cq8a9-0008MK-8V for 24603@debbugs.gnu.org; Mon, 20 Mar 2017 21:27:29 -0400 Original-Received: by mail-wm0-f41.google.com with SMTP id n11so1641083wma.1 for <24603@debbugs.gnu.org>; Mon, 20 Mar 2017 18:27:29 -0700 (PDT) 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=YkX3UtYmRP7I9NG4nz77Ih+XDQSqEWCu8SxxmZVUhgg=; b=qqeYZJB5jQAFW5oxba3H4dCDj7Hpyg33N2+peXG+1GPDzUFTO46jcAUD2UrW20qOHD B87jsd+Rr+hvfjFsZYGENtx9+TNjoOfbk3mJz7pe64wT5wjlI24ZOoneT/HH3lv7+nIr YKr7KxYxwfP6K4DldmEezXPo1l0YBR3OcJ+Tf+A++5S3kh3KFqS4kjk3/zvNNHySTVML UXUQYiGOnKXBKAd1R6Tcr8IZ2IScl5BPHHt3WKtPngWbgT5sUEiHw+xjY00ZZa9gGpUH cfCyg0qD3MQ+l+tBljRC9+1CUd2jYufOLXUwFpXSdjuem+fStIvEd7ULZ7buMTIg1s7i 96Dg== 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=YkX3UtYmRP7I9NG4nz77Ih+XDQSqEWCu8SxxmZVUhgg=; b=hJjvCoxciDOQzYFvjNPSzhXpRE8TP7HZtpfKsBsizvXnx9x0t3cOR2hvbBbjQzzKmK XxT+k9ZX1B0uhc1BEW4FVVdkdu5zh600tB2PUNyIoar/YrB19wHYR6Ft2JMGG8qMxm7Z tO8fw1tiDveRebgq3AP7uVBwARCmfD5k4l+aTmF44GtayzrvM1DAMOg2Rn5l28GwGwVI hOyVXlhkw+1YrwqN/NuoukqHqTWzBO5DTSuqCbVeK8042RpGZlscF4Bw8RfDDVKeG1+9 i/7ShQKnMu4FgI5ztXVXQtucnxwCmP3+mdpntdHKPCPS3f89TuEJM6M/KuoV5iZuN+Lf Sx0Q== X-Gm-Message-State: AFeK/H1E0nNHYI4ASEOqWUB/G/OYJbNhCemY8V0S7UnfNiHVM0lzRlun/4H5mKm4Hl2f3ItG X-Received: by 10.28.210.133 with SMTP id j127mr358982wmg.64.1490059643382; Mon, 20 Mar 2017 18:27:23 -0700 (PDT) Original-Received: from mpn.zrh.corp.google.com ([2620:0:105f:303:5db2:6e42:d9c7:408e]) by smtp.gmail.com with ESMTPSA id z88sm22911216wrb.26.2017.03.20.18.27.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Mar 2017 18:27:18 -0700 (PDT) Original-Received: by mpn.zrh.corp.google.com (Postfix, from userid 126942) id 4986B1E0286; Tue, 21 Mar 2017 02:27:14 +0100 (CET) X-Mailer: git-send-email 2.12.1.500.gab5fba24ee-goog In-Reply-To: <20170321012709.19402-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:130768 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 a83469cbdc7..b1a5f8e236e 100644 --- a/src/casefiddle.c +++ b/src/casefiddle.c @@ -231,102 +231,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.367.g23dc2f6d3c-goog