[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Use non-destructive parsing in pkl_struct_type_traverse
From: |
Tim Rühsen |
Subject: |
[PATCH] Use non-destructive parsing in pkl_struct_type_traverse |
Date: |
Thu, 9 Apr 2020 16:50:34 +0200 |
This removes the need for memory allocations and simplifies code.
2020-04-09 Tim Rühsen <address@hidden>
* lib/pkl-ast.c (pkl_struct_type_traverse): Use
non-destructive parsing without memory allocations.
---
ChangeLog | 5 +++++
lib/pkl-ast.c | 46 ++++++++++++----------------------------------
2 files changed, 17 insertions(+), 34 deletions(-)
diff --git a/lib/pkl-ast.c b/lib/pkl-ast.c
index 78e7261a..cacc1aa0 100644
--- a/lib/pkl-ast.c
+++ b/lib/pkl-ast.c
@@ -645,35 +645,24 @@ pkl_ast_dup_type (pkl_ast_node type)
pkl_ast_node
pkl_struct_type_traverse (pkl_ast_node type, const char *path)
{
- char *trunk, *sub, *base;
+ const char *s, *e;
if (PKL_AST_TYPE_CODE (type) != PKL_TYPE_STRUCT)
return NULL;
- trunk = strndup (path, strlen (path) - strlen (strrchr (path, '.')));
- base = strtok (trunk, ".");
-
- /* Node in the form XX. Check to silence the compiler about base not used */
- if (base == NULL)
- {
- free (trunk);
- return type;
- }
-
- while ((sub = strtok (NULL, ".")) != NULL)
+ for (s = path, e = s; *e; s = e + 1)
{
- pkl_ast_node ename;
- pkl_ast_node etype, t;
- char *field;
+ /* Ignore empty entries and ignore last part */
+ if ((e = strchrnul (s, ':')) == s || !*e)
+ continue;
- etype = NULL;
-
- if (PKL_AST_TYPE_CODE (type) != PKL_TYPE_STRUCT)
- goto out;
-
- for (t = PKL_AST_TYPE_S_ELEMS (type); t;
+ for (pkl_ast_node t = PKL_AST_TYPE_S_ELEMS (type); t;
t = PKL_AST_CHAIN (t))
{
+ pkl_ast_node ename;
+ pkl_ast_node etype;
+ const char *field;
+
if (PKL_AST_CODE (t) != PKL_AST_STRUCT_TYPE_FIELD)
continue;
@@ -682,22 +671,11 @@ pkl_struct_type_traverse (pkl_ast_node type, const char
*path)
field = PKL_AST_IDENTIFIER_POINTER (ename);
- if (STREQ (field, sub))
- {
- type = etype;
- break;
- }
+ if (!strncmp (field, s, e - s) && !field[e - s])
+ return etype;
}
-
- if (type != etype)
- goto out;
}
- free (trunk);
- return type;
-
-out:
- free (trunk);
return NULL;
}
--
2.26.0
- [PATCH] Use non-destructive parsing in pkl_struct_type_traverse,
Tim Rühsen <=