(root)/
grep-3.11/
src/
searchutils.c
       1  /* searchutils.c - helper subroutines for grep's matchers.
       2     Copyright 1992, 1998, 2000, 2007, 2009-2023 Free Software Foundation, Inc.
       3  
       4     This program is free software; you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation; either version 3, or (at your option)
       7     any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program; if not, write to the Free Software
      16     Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
      17     02110-1301, USA.  */
      18  
      19  #include <config.h>
      20  
      21  #define SEARCH_INLINE _GL_EXTERN_INLINE
      22  #define SYSTEM_INLINE _GL_EXTERN_INLINE
      23  #include "search.h"
      24  
      25  /* For each byte B, sbwordchar[B] is true if B is a single-byte
      26     character that is a word constituent, and is false otherwise.  */
      27  static bool sbwordchar[NCHAR];
      28  
      29  /* Whether -w considers WC to be a word constituent.  */
      30  static bool
      31  wordchar (wint_t wc)
      32  {
      33    return wc == L'_' || iswalnum (wc);
      34  }
      35  
      36  void
      37  wordinit (void)
      38  {
      39    for (int i = 0; i < NCHAR; i++)
      40      sbwordchar[i] = wordchar (localeinfo.sbctowc[i]);
      41  }
      42  
      43  kwset_t
      44  kwsinit (bool mb_trans)
      45  {
      46    char *trans = NULL;
      47  
      48    if (match_icase && (MB_CUR_MAX == 1 || mb_trans))
      49      {
      50        trans = ximalloc (NCHAR);
      51        /* If I is a single-byte character that becomes a different
      52           single-byte character when uppercased, set trans[I]
      53           to that character.  Otherwise, set trans[I] to I.  */
      54        for (int i = 0; i < NCHAR; i++)
      55          trans[i] = toupper (i);
      56      }
      57  
      58    return kwsalloc (trans);
      59  }
      60  
      61  /* Return the number of bytes needed to go back to the start of a
      62     multibyte character in a buffer.  The buffer starts at *MB_START.
      63     (See below for MBCLEN's role.)  The multibyte character contains
      64     the byte addressed by CUR.  The buffer ends just before END, which
      65     must not be less than CUR.
      66  
      67     If CUR is no larger than *MB_START, return CUR - *MB_START without
      68     modifying *MB_START or dealing with MBCLEN.  Otherwise, update
      69     *MB_START to point to the first multibyte character starting on or
      70     after CUR, and if MBCLEN is nonnull then deal with MBCLEN as follows:
      71  
      72       - If this function returns 0 and the locale is multibyte and is
      73         not UTF-8, set *MBCLEN to the number of bytes in the multibyte
      74         character containing the byte addressed by (CUR - 1).
      75  
      76       - Otherwise, possibly set *MBCLEN to an unspecified value.
      77  
      78     *MB_START should point to the start of a multibyte character, or to
      79     an encoding-error byte.
      80  
      81     *END should be a sentinel byte - one of '\0', '\r', '\n', '.', '/',
      82     which POSIX says cannot be part of any other character.  Also,
      83     there should be a byte string immediately before *MB_START that
      84     contains a sentinel byte.  This means it is OK to scan backwards
      85     before *MB_START as long as the scan stops at a sentinel byte, and
      86     similarly it is OK to scan forwards from CUR (without checking END)
      87     so long as the scan stops at a sentinel byte.
      88  
      89     Treat encoding errors as if they were single-byte characters.  */
      90  ptrdiff_t
      91  mb_goback (char const **mb_start, idx_t *mbclen, char const *cur,
      92             char const *end)
      93  {
      94    const char *p = *mb_start;
      95    const char *p0 = p;
      96  
      97    if (cur <= p)
      98      return cur - p;
      99  
     100    if (localeinfo.using_utf8)
     101      {
     102        /* UTF-8 permits scanning backward to the previous character.
     103           Start by assuming CUR is at a character boundary.  */
     104        p = cur;
     105  
     106        if ((*cur & 0xc0) == 0x80)
     107          for (int i = 1; i <= 3; i++)
     108            if ((cur[-i] & 0xc0) != 0x80)
     109              {
     110                /* True if the length implied by the putative byte 1 at
     111                   CUR[-I] extends at least through *CUR.  */
     112                bool long_enough = (~cur[-i] & 0xff) >> (7 - i) == 0;
     113  
     114                if (long_enough)
     115                  {
     116                    mbstate_t mbs = { 0 };
     117                    ptrdiff_t clen = imbrlen (cur - i, end - (cur - i), &mbs);
     118                    if (0 <= clen)
     119                      {
     120                        /* This multibyte character contains *CUR.  */
     121                        p0 = cur - i;
     122                        p = p0 + clen;
     123                      }
     124                  }
     125                break;
     126              }
     127      }
     128    else
     129      {
     130        /* In non-UTF-8 encodings, to find character boundaries one must
     131           in general scan forward from the start of the buffer.  */
     132        mbstate_t mbs = { 0 };
     133        ptrdiff_t clen;
     134  
     135        do
     136          {
     137            clen = mb_clen (p, end - p, &mbs);
     138  
     139            if (clen < 0)
     140              {
     141                /* An invalid sequence, or a truncated multibyte character.
     142                   Treat it as a single byte character.  */
     143                clen = 1;
     144                memset (&mbs, 0, sizeof mbs);
     145              }
     146            p0 = p;
     147            p += clen;
     148          }
     149        while (p < cur);
     150  
     151        if (mbclen)
     152          *mbclen = clen;
     153      }
     154  
     155    *mb_start = p;
     156    return p == cur ? 0 : cur - p0;
     157  }
     158  
     159  /* Examine the start of BUF (which goes to END) for word constituents.
     160     If COUNTALL, examine as many as possible; otherwise, examine at most one.
     161     Return the total number of bytes in the examined characters.  */
     162  static idx_t
     163  wordchars_count (char const *buf, char const *end, bool countall)
     164  {
     165    idx_t n = 0;
     166    mbstate_t mbs = { 0 };
     167    while (n < end - buf)
     168      {
     169        unsigned char b = buf[n];
     170        if (sbwordchar[b])
     171          n++;
     172        else if (localeinfo.sbclen[b] != -2)
     173          break;
     174        else
     175          {
     176            wchar_t wc = 0;
     177            size_t wcbytes = mbrtowc (&wc, buf + n, end - buf - n, &mbs);
     178            if (!wordchar (wc))
     179              break;
     180            n += wcbytes + !wcbytes;
     181          }
     182        if (!countall)
     183          break;
     184      }
     185    return n;
     186  }
     187  
     188  /* Examine the start of BUF for the longest prefix containing just
     189     word constituents.  Return the total number of bytes in the prefix.
     190     The buffer ends at END.  */
     191  idx_t
     192  wordchars_size (char const *buf, char const *end)
     193  {
     194    return wordchars_count (buf, end, true);
     195  }
     196  
     197  /* If BUF starts with a word constituent, return the number of bytes
     198     used to represent it; otherwise, return zero.  The buffer ends at END.  */
     199  idx_t
     200  wordchar_next (char const *buf, char const *end)
     201  {
     202    return wordchars_count (buf, end, false);
     203  }
     204  
     205  /* In the buffer BUF, return nonzero if the character whose encoding
     206     contains the byte before CUR is a word constituent.  The buffer
     207     ends at END.  */
     208  idx_t
     209  wordchar_prev (char const *buf, char const *cur, char const *end)
     210  {
     211    if (buf == cur)
     212      return 0;
     213    unsigned char b = *--cur;
     214    if (! localeinfo.multibyte || localeinfo.using_utf8 & ~(b >> 7))
     215      return sbwordchar[b];
     216    char const *p = buf;
     217    cur -= mb_goback (&p, NULL, cur, end);
     218    return wordchar_next (cur, end);
     219  }