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: [RFC 07/18] Split up casify_region function. Date: Tue, 4 Oct 2016 03:10:30 +0200 Message-ID: <1475543441-10493-7-git-send-email-mina86@mina86.com> References: <1475543441-10493-1-git-send-email-mina86@mina86.com> NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1475543631 32278 195.159.176.226 (4 Oct 2016 01:13:51 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 4 Oct 2016 01:13:51 +0000 (UTC) To: 24603@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Oct 04 03:13:47 2016 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 1brEIZ-0006TZ-3x for geb-bug-gnu-emacs@m.gmane.org; Tue, 04 Oct 2016 03:13:35 +0200 Original-Received: from localhost ([::1]:39721 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1brEIW-0007es-9l for geb-bug-gnu-emacs@m.gmane.org; Mon, 03 Oct 2016 21:13:32 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56514) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1brEHB-0006nT-Rf for bug-gnu-emacs@gnu.org; Mon, 03 Oct 2016 21:12:14 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1brEH6-0002RV-6A for bug-gnu-emacs@gnu.org; Mon, 03 Oct 2016 21:12:08 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:37365) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1brEH6-0002RP-3I for bug-gnu-emacs@gnu.org; Mon, 03 Oct 2016 21:12:04 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1brEH5-0006jW-V9 for bug-gnu-emacs@gnu.org; Mon, 03 Oct 2016 21:12:03 -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, 04 Oct 2016 01:12:03 +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: Original-Received: via spool by 24603-submit@debbugs.gnu.org id=B24603.147554346825650 (code B ref 24603); Tue, 04 Oct 2016 01:12:03 +0000 Original-Received: (at 24603) by debbugs.gnu.org; 4 Oct 2016 01:11:08 +0000 Original-Received: from localhost ([127.0.0.1]:43527 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1brEGB-0006fR-5D for submit@debbugs.gnu.org; Mon, 03 Oct 2016 21:11:08 -0400 Original-Received: from mail-wm0-f54.google.com ([74.125.82.54]:37805) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1brEG4-0006cW-TY for 24603@debbugs.gnu.org; Mon, 03 Oct 2016 21:11:01 -0400 Original-Received: by mail-wm0-f54.google.com with SMTP id b201so114000995wmb.0 for <24603@debbugs.gnu.org>; Mon, 03 Oct 2016 18:11:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=EeUg8Ke5Z9oX+PONc3vK5+ZjxacC0HMR5ZF5fcE84Ow=; b=jfhPPx8rUWe+cogtCrjzb+s99e1u6fkaNGE/DaqvdM+PvsI+4IHv5ZwQQWlM+zLZrK 2jodPB1MJp4QHrK+oAHflmuHuCkmo9sDgy7dxv3l8AytSoIeEf9rYtRiNH2EDOc4DAKB UhHJ++lWQK6uqAmmpTpaOfGAFHNsF3NZDU5V/l8BRySTHT3OjCQ7CwTKfemnzktJCmzD xfMPTezyCrilEyFlSf23jUHh+qo+wVHjzlcg0PIHjrcTmA3+MZc4aIKBogRXn9kWEmMy 1f/lw/c8vcoKzsansATzKLxMMoF+/IMxQmjcysAl2xlPwWvfvnzmFM4u8VSZ3jaYIlxV 7THg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references; bh=EeUg8Ke5Z9oX+PONc3vK5+ZjxacC0HMR5ZF5fcE84Ow=; b=mKCt/moPe/Z28Ri3N5ZZL0dfF8tPH1adLQ+AzTuVSTvAW1UUk3u3efwEVWrnnmphuO 3vnX7rIB25a+5DICem38YN8RDMbNC/8T/uU5Yk1F/ReBNXRvWzKVf7CMOwPfpRVhIRIv OzkI+UWiTb+4Iu2WOrXhCDHLbT0OtPhqrFe1H9Yk00q5JTZOHT9Nd0ky1tyOc33fgOAE j2cJo5g6OVnyMQb3T5ONPmnqdjGQeo/Ruf7ynVAHULJvc1vDxgqVPM8rT/RXbUQmc+9l jN081DzeXm7k6ZofCNP8ogh3j+s68BoGQhXtW3FcFdHq7mgsnQn5hVjSDM0Soy2nXoXQ Q9og== X-Gm-Message-State: AA6/9RlGqanbqySM9MmAZYeIVYYHyDSVOBV9REplr0A27hrOhSeac9vi720YPqwAT+TnTOC3 X-Received: by 10.28.234.5 with SMTP id i5mr1032919wmh.128.1475543453790; Mon, 03 Oct 2016 18:10:53 -0700 (PDT) Original-Received: from mpn.zrh.corp.google.com ([172.16.113.135]) by smtp.gmail.com with ESMTPSA id o196sm20556632wmg.8.2016.10.03.18.10.51 for <24603@debbugs.gnu.org> (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Oct 2016 18:10:52 -0700 (PDT) Original-Received: by mpn.zrh.corp.google.com (Postfix, from userid 126942) id B3CA41E0289; Tue, 4 Oct 2016 03:10:48 +0200 (CEST) X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 In-Reply-To: <1475543441-10493-1-git-send-email-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:123994 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 b3ffa86..a016871 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 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_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 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_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.8.0.rc3.226.g39d4020