texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

branch master updated: * tp/Texinfo/XS/main/translations.c (replace_subs


From: Patrice Dumas
Subject: branch master updated: * tp/Texinfo/XS/main/translations.c (replace_substrings), tp/Texinfo/XS/main/utils.c (read_flag_len), tp/Texinfo/XS/parsetexi/end_line.c (end_line_starting_block), tp/Texinfo/XS/parsetexi/handle_commands.c (parse_rawline_command), tp/Texinfo/XS/parsetexi/parser.c (process_remaining_on_line): replace read_flag_name by read_flag_len that only returns the length of the flag. This allows to check first if there is a matching brace or if there is nothing remaining on the line before allocating [...]
Date: Mon, 27 May 2024 17:12:27 -0400

This is an automated email from the git hooks/post-receive script.

pertusus pushed a commit to branch master
in repository texinfo.

The following commit(s) were added to refs/heads/master by this push:
     new 27364b21ca * tp/Texinfo/XS/main/translations.c (replace_substrings), 
tp/Texinfo/XS/main/utils.c (read_flag_len), tp/Texinfo/XS/parsetexi/end_line.c 
(end_line_starting_block), tp/Texinfo/XS/parsetexi/handle_commands.c 
(parse_rawline_command), tp/Texinfo/XS/parsetexi/parser.c 
(process_remaining_on_line): replace read_flag_name by read_flag_len that only 
returns the length of the flag.  This allows to check first if there is a 
matching brace or if there is nothing remaining on the l [...]
27364b21ca is described below

commit 27364b21cab5347b7eb3e02b0af1735815172d8e
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Mon May 27 23:12:14 2024 +0200

    * tp/Texinfo/XS/main/translations.c (replace_substrings),
    tp/Texinfo/XS/main/utils.c (read_flag_len),
    tp/Texinfo/XS/parsetexi/end_line.c (end_line_starting_block),
    tp/Texinfo/XS/parsetexi/handle_commands.c (parse_rawline_command),
    tp/Texinfo/XS/parsetexi/parser.c (process_remaining_on_line): replace
    read_flag_name by read_flag_len that only returns the length of the
    flag.  This allows to check first if there is a matching brace or if
    there is nothing remaining on the line before allocating the memory
    for the flag.  Update callers.
    
    * tp/Texinfo/XS/parsetexi/parser.c (new_value_element)
    (process_remaining_on_line): add length of flag as an argument to
    new_value_element.  Update callers.
    
    * tp/Texinfo/XS/parsetexi/parser.c (abort_empty_line)
    (isolate_last_space_internal): use text_append_n instead of
    text_append.
    
    * tp/Texinfo/XS/parsetexi/parser.c (isolate_last_space_internal):
    reuse text_len instead of calling strlen.
---
 ChangeLog                                 | 23 +++++++++
 tp/Texinfo/XS/main/translations.c         | 14 +++---
 tp/Texinfo/XS/main/utils.c                | 15 +++---
 tp/Texinfo/XS/main/utils.h                |  2 +-
 tp/Texinfo/XS/parsetexi/end_line.c        |  9 ++--
 tp/Texinfo/XS/parsetexi/handle_commands.c | 13 +++--
 tp/Texinfo/XS/parsetexi/parser.c          | 80 +++++++++++++++++--------------
 7 files changed, 95 insertions(+), 61 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c56c1e3b42..51345a61c4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,26 @@
+2024-05-27  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/Texinfo/XS/main/translations.c (replace_substrings),
+       tp/Texinfo/XS/main/utils.c (read_flag_len),
+       tp/Texinfo/XS/parsetexi/end_line.c (end_line_starting_block),
+       tp/Texinfo/XS/parsetexi/handle_commands.c (parse_rawline_command),
+       tp/Texinfo/XS/parsetexi/parser.c (process_remaining_on_line): replace
+       read_flag_name by read_flag_len that only returns the length of the
+       flag.  This allows to check first if there is a matching brace or if
+       there is nothing remaining on the line before allocating the memory
+       for the flag.  Update callers.
+
+       * tp/Texinfo/XS/parsetexi/parser.c (new_value_element)
+       (process_remaining_on_line): add length of flag as an argument to
+       new_value_element.  Update callers.
+
+       * tp/Texinfo/XS/parsetexi/parser.c (abort_empty_line)
+       (isolate_last_space_internal): use text_append_n instead of
+       text_append.
+
+       * tp/Texinfo/XS/parsetexi/parser.c (isolate_last_space_internal):
+       reuse text_len instead of calling strlen.
+
 2024-05-27  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/XS/parsetexi/macro.c (handle_macro): use strchrnul
diff --git a/tp/Texinfo/XS/main/translations.c 
b/tp/Texinfo/XS/main/translations.c
index 83313c2d23..f69a102575 100644
--- a/tp/Texinfo/XS/main/translations.c
+++ b/tp/Texinfo/XS/main/translations.c
@@ -335,7 +335,7 @@ replace_substrings (const char *string,
       if (q)
         {
           int found = 0;
-          char *flag;
+          size_t flag_len;
 
           if (q - p)
             text_append_n (&substituted, p, q - p);
@@ -343,14 +343,16 @@ replace_substrings (const char *string,
           p = q;
           /* past { */
           q++;
-          flag = read_flag_name (&q);
-          if (flag)
+          flag_len = read_flag_len (q);
+          if (flag_len)
             {
-              if (*q == '}')
+              if (*(q + flag_len) == '}')
                 {
                   int i;
+                  char *flag = strndup (q, flag_len);
+
                   /* past } */
-                  q++;
+                  q += flag_len +1;
                   for (i = 0; i < replaced_substrings->number; i++)
                     {
                       if (!strcmp (replaced_substrings->list[i].name,
@@ -362,8 +364,8 @@ replace_substrings (const char *string,
                           break;
                         }
                     }
+                  free (flag);
                 }
-              free (flag);
             }
           if (!found)
             text_append_n (&substituted, p, q - p);
diff --git a/tp/Texinfo/XS/main/utils.c b/tp/Texinfo/XS/main/utils.c
index 41d5f7ae93..1df76e737c 100644
--- a/tp/Texinfo/XS/main/utils.c
+++ b/tp/Texinfo/XS/main/utils.c
@@ -696,12 +696,12 @@ index_number_index_by_name (const SORTED_INDEX_NAMES 
*sorted_indices,
 
 
 /* text parsing functions used in diverse situations */
-/* Read a name used for @set, @value and translations arguments. */
-char *
-read_flag_name (const char **ptr)
+/* Determine if there is a name used for @set, @value and translations
+   arguments and its length. */
+size_t
+read_flag_len (const char *text)
 {
-  const char *p = *ptr, *q;
-  char *ret = 0;
+  const char *p = text, *q;
 
   q = p;
   if (!isascii_alnum (*q) && *q != '-' && *q != '_')
@@ -710,11 +710,8 @@ read_flag_name (const char **ptr)
   while (!strchr (whitespace_chars, *q)
          && !strchr ("{\\}~`^+\"<>|@", *q))
     q++;
-  ret = strndup (p, q - p);
-  p = q;
 
-  *ptr = p;
-  return ret;
+  return q - p;
 }
 
 /* s/\s+/ /g with re => '/a' in perl */
diff --git a/tp/Texinfo/XS/main/utils.h b/tp/Texinfo/XS/main/utils.h
index e8f1a33c1a..b6102a2341 100644
--- a/tp/Texinfo/XS/main/utils.h
+++ b/tp/Texinfo/XS/main/utils.h
@@ -188,7 +188,7 @@ INDEX *indices_info_index_by_name (const INDEX_LIST 
*indices_information,
 INDEX *ultimate_index (INDEX *index);
 size_t index_number_index_by_name (const SORTED_INDEX_NAMES *sorted_indices,
                                    const char *name);
-char *read_flag_name (const char **ptr);
+size_t read_flag_len (const char *text);
 int section_level (const ELEMENT *section);
 enum command_id section_level_adjusted_command_name (const ELEMENT *element);
 char *collapse_spaces (const char *text);
diff --git a/tp/Texinfo/XS/parsetexi/end_line.c 
b/tp/Texinfo/XS/parsetexi/end_line.c
index d16ee73ab7..bda394d1ef 100644
--- a/tp/Texinfo/XS/parsetexi/end_line.c
+++ b/tp/Texinfo/XS/parsetexi/end_line.c
@@ -27,7 +27,7 @@
 #include "tree.h"
 #include "debug_parser.h"
 #include "errors_parser.h"
-/* for isascii_alnum whitespace_chars read_flag_name
+/* for isascii_alnum whitespace_chars read_flag_len
    indices_info_index_by_name ultimate_index fatal */
 #include "utils.h"
 /* for parse_node_manual */
@@ -1108,9 +1108,10 @@ end_line_starting_block (ELEMENT *current)
                     }
                   else
                     {
-                      char *flag = read_flag_name (&p);
-                      if (flag && !*p)
+                      size_t flag_len = read_flag_len (p);
+                      if (flag_len && !*(p + flag_len))
                         {
+                          char *flag = strndup (p, flag_len);
                           bad_line = 0;
                           if (command == CM_ifclear || command == CM_ifset)
                             {
@@ -1131,8 +1132,8 @@ end_line_starting_block (ELEMENT *current)
                             }
                           debug ("CONDITIONAL @%s %s: %d",
                                  command_name(command), flag, iftrue);
+                          free (flag);
                         }
-                      free (flag);
                     }
                 }
             }
diff --git a/tp/Texinfo/XS/parsetexi/handle_commands.c 
b/tp/Texinfo/XS/parsetexi/handle_commands.c
index b3d80023a4..5a6d1d1641 100644
--- a/tp/Texinfo/XS/parsetexi/handle_commands.c
+++ b/tp/Texinfo/XS/parsetexi/handle_commands.c
@@ -25,7 +25,7 @@
 #include "command_ids.h"
 #include "element_types.h"
 #include "text.h"
-/* for isascii_alnum whitespace_chars read_flag_name item_line_parent */
+/* for isascii_alnum whitespace_chars read_flag_len item_line_parent */
 #include "utils.h"
 #include "counter.h"
 #include "command_stack.h"
@@ -204,7 +204,7 @@ parse_rawline_command (const char *line, enum command_id 
cmd,
                    "{\\}~^+\"<>|@"); /* other bytes that aren't allowed */
       if (q)
         {
-        /* see also read_flag_name function in utils.c */
+        /* see also read_flag_len function in utils.c */
           r = skip_to_comment_if_comment_or_spaces (q, has_comment);
           if (!r)
             goto set_invalid;
@@ -238,19 +238,22 @@ parse_rawline_command (const char *line, enum command_id 
cmd,
     case CM_clear:
       {
       char *flag = 0;
+      size_t flag_len;
       p = line;
       p += strspn (p, whitespace_chars);
       if (!*p)
         goto clear_no_name;
       q = p;
-      flag = read_flag_name (&q);
-      if (!flag)
+      flag_len = read_flag_len (p);
+      if (!flag_len)
         goto clear_invalid;
+      q = p + flag_len;
       r = skip_to_comment_if_comment_or_spaces (q, has_comment);
       if (!r || r != q)
         goto clear_invalid; /* Trailing argument. */
 
-      ADD_ARG (p, q - p);
+      ADD_ARG (p, flag_len);
+      flag = strndup (p, flag_len);
       clear_value (flag);
       free (flag);
 
diff --git a/tp/Texinfo/XS/parsetexi/parser.c b/tp/Texinfo/XS/parsetexi/parser.c
index a8ef4e2a31..f965861c6a 100644
--- a/tp/Texinfo/XS/parsetexi/parser.c
+++ b/tp/Texinfo/XS/parsetexi/parser.c
@@ -23,7 +23,7 @@
 #include "element_types.h"
 #include "tree_types.h"
 #include "tree.h"
-/* for isascii_alnum, whitespace_chars, read_flag_name, item_line_parent
+/* for isascii_alnum, whitespace_chars, read_flag_len, item_line_parent
    delete_global_info, parse_line_directive, count_multibyte */
 #include "utils.h"
 /* for relocate_source_marks */
@@ -647,7 +647,9 @@ end_preformatted (ELEMENT *current,
   return current;
 }
 
-/* Add TEXT to the contents of CURRENT, maybe starting a new paragraph.
+/* Add LEN_TEXT of TEXT to the contents of CURRENT, maybe starting a new
+   paragraph.
+   TEXT may not have a NUL character at TEXT + LEN_TEXT.
    If TRANSFER_MARKS_ELEMENT is given, also transfer mark sources
    from that element.
    */
@@ -750,7 +752,10 @@ merge_text (ELEMENT *current, const char *text, size_t 
len_text,
 }
 
 /* If last contents child of CURRENT is an empty line element, remove
-   or merge text, and return true. */
+   or merge text, and return true.
+   If LEN_TEXT is set, add LEN_TEXT of ADDITIONAL_SPACES to the
+   last contents child of CURRENT.
+ */
 int
 abort_empty_line (ELEMENT **current_inout, const char *additional_spaces,
                   size_t len_text)
@@ -820,7 +825,7 @@ abort_empty_line (ELEMENT **current_inout, const char 
*additional_spaces,
 
           owning_element = lookup_extra_element (last_child,
                                                  "spaces_associated_command");
-          text_append (&spaces_element->text, e->text.text);
+          text_append_n (&spaces_element->text, e->text.text, e->text.end);
           transfer_source_marks (e, spaces_element);
           add_info_element_oot (owning_element, "spaces_before_argument",
                                 spaces_element);
@@ -850,7 +855,7 @@ isolate_last_space_internal (ELEMENT *current)
   /* If text all whitespace */
   if (text[strspn (text, whitespace_chars)] == '\0')
     {
-      text_append (&spaces_element->text, last_elt->text.text);
+      text_append_n (&spaces_element->text, text, text_len);
       transfer_source_marks (last_elt, spaces_element);
       add_info_element_oot (current, "spaces_after_argument",
                             spaces_element);
@@ -864,7 +869,7 @@ isolate_last_space_internal (ELEMENT *current)
       text_reset (&t);
 
       trailing_spaces = 0;
-      for (i = strlen (text) - 1;
+      for (i = text_len - 1;
            i > 0 && strchr (whitespace_chars, text[i]);
            i--)
         trailing_spaces++;
@@ -1080,8 +1085,9 @@ gather_spaces_after_cmd_before_arg (ELEMENT *current)
                         spaces_element);
 }
 
-ELEMENT *
-new_value_element (enum command_id cmd, char *flag, ELEMENT *spaces_element)
+static ELEMENT *
+new_value_element (enum command_id cmd, const char *flag,
+                   int flag_len, ELEMENT *spaces_element)
 {
   ELEMENT *value_elt = new_element (ET_NONE);
   ELEMENT *brace_command_arg = new_element (ET_brace_command_arg);
@@ -1089,7 +1095,7 @@ new_value_element (enum command_id cmd, char *flag, 
ELEMENT *spaces_element)
 
   value_elt->cmd = cmd;
 
-  text_append (&value_text->text, flag);
+  text_append_n (&value_text->text, flag, flag_len);
   add_to_element_args (value_elt, brace_command_arg);
   add_to_element_contents (brace_command_arg, value_text);
   if (spaces_element)
@@ -1806,23 +1812,24 @@ process_remaining_on_line (ELEMENT **current_inout, 
const char **line_inout)
         }
       if (*remaining_line == '{')
         {
-          char *flag;
+          size_t flag_len;
 
           remaining_line++;
-          flag = read_flag_name (&remaining_line);
-          if (flag)
+          flag_len = read_flag_len (remaining_line);
+          if (flag_len)
             {
-              if (*remaining_line == '}')
+              if (*(remaining_line + flag_len) == '}')
                 {
-                  char *value;
-                  value = fetch_value (flag);
+                  char *flag = strndup (remaining_line, flag_len);
+                  char *value = fetch_value (flag);
+
+                  remaining_line += flag_len +1; /* past '}' */
 
                   if (value)
                     {
                       SOURCE_MARK *value_source_mark;
                       ELEMENT *sm_value_element;
 
-                      remaining_line++; /* past '}' */
                       if (global_parser_conf.max_macro_call_nesting
                           && value_expansion_nr
                                   >= global_parser_conf.max_macro_call_nesting)
@@ -1838,6 +1845,10 @@ process_remaining_on_line (ELEMENT **current_inout, 
const char **line_inout)
                           goto funexit;
                         }
 
+                      sm_value_element
+                        = new_value_element (cmd, flag, flag_len,
+                                             spaces_element);
+
                       input_push_text (strdup (remaining_line),
                                        current_source_info.line_nr, 0, 0);
                       input_push_text (strdup (value),
@@ -1847,8 +1858,6 @@ process_remaining_on_line (ELEMENT **current_inout, const 
char **line_inout)
                           = new_source_mark (SM_type_value_expansion);
                       value_source_mark->status = SM_status_start;
                       value_source_mark->line = strdup (value);
-                      sm_value_element = new_value_element (cmd, flag,
-                                                            spaces_element);
                       value_source_mark->element = sm_value_element;
 
                       register_source_mark (current, value_source_mark);
@@ -1858,15 +1867,14 @@ process_remaining_on_line (ELEMENT **current_inout, 
const char **line_inout)
 
                       /* Move 'line' to end of string so next input to
                          be processed is taken from input stack. */
-                      line += (remaining_line - line) + strlen 
(remaining_line);
+                      line = remaining_line + strlen (remaining_line);
                     }
+                  free (flag);
                   if (value)
                     {
-                      free (flag);
                       goto funexit;
                     }
                 }
-              free (flag);
             }
         }
       if (spaces_element)
@@ -2109,8 +2117,7 @@ process_remaining_on_line (ELEMENT **current_inout, const 
char **line_inout)
       /* @value not expanded (expansion is done above), and @txiinternalvalue 
*/
       if ((cmd == CM_value) || (cmd == CM_txiinternalvalue))
         {
-          const char *arg_start;
-          char *flag;
+          size_t flag_len;
           ELEMENT *spaces_element = 0;
           if (global_parser_conf.ignore_space_after_braced_command_name)
             {
@@ -2127,24 +2134,23 @@ process_remaining_on_line (ELEMENT **current_inout, 
const char **line_inout)
             goto value_invalid;
 
           line++;
-          arg_start = line;
-          flag = read_flag_name (&line);
-          if (!flag)
+          flag_len = read_flag_len (line);
+          if (!flag_len)
             goto value_invalid;
 
-          if (*line != '}')
+          if (*(line + flag_len) != '}')
             {
-              line = arg_start - 1;
-              free (flag);
+              line--;
               goto value_invalid;
             }
 
           if (1) /* @value syntax is valid */
             {
-              char *value;
           value_valid:
               if (cmd == CM_value)
                 {
+                  char *value;
+                  char *flag = strndup (line, flag_len);
                   value = fetch_value (flag);
                   if (!value)
                     {
@@ -2157,10 +2163,12 @@ process_remaining_on_line (ELEMENT **current_inout, 
const char **line_inout)
 
                       line_warn ("undefined flag: %s", flag);
 
-                      value_elt = new_value_element (cmd, flag, 
spaces_element);
+                      value_elt
+                         = new_value_element (cmd, flag, flag_len,
+                                              spaces_element);
                       add_to_element_contents (current, value_elt);
 
-                      line++; /* past '}' */
+                      line += flag_len +1; /* past '}' */
                     }
                   else
                     {
@@ -2179,14 +2187,14 @@ process_remaining_on_line (ELEMENT **current_inout, 
const char **line_inout)
 
                   abort_empty_line (&current, NULL, 0);
 
-                  txiinternalvalue_elt = new_value_element (cmd, flag,
-                                                            spaces_element);
+                  txiinternalvalue_elt
+                    = new_value_element (cmd, line, flag_len,
+                                         spaces_element);
 
                   add_to_element_contents (current, txiinternalvalue_elt);
 
-                  line++; /* past '}' */
+                  line += flag_len +1; /* past '}' */
 
-                  free (flag);
                   goto funexit;
                 }
             }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]