From 71607446d4a16bd46945d1011d149060e7f898ca Mon Sep 17 00:00:00 2001 From: Dmitrii Date: Fri, 10 Jan 2025 13:30:46 +0800 Subject: [PATCH] add regex for bool parsing & test struct w/ bool parsing (#169) GitHub: fix GH-168 Struct parsing invokes "parse_ctype" on the whole member signature, which fails if member type is "bool" due to plain string matching for it. This change updates "bool" type matching to a regexp, so TYPE_BOOL is correctly parsed for a whole signature like "bool toggle" as well as just "bool". --------- Co-authored-by: Sutou Kouhei --- lib/fiddle/cparser.rb | 2 +- test/fiddle/test_cparser.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/fiddle/cparser.rb b/lib/fiddle/cparser.rb index 264ca166..cdd139d5 100644 --- a/lib/fiddle/cparser.rb +++ b/lib/fiddle/cparser.rb @@ -247,7 +247,7 @@ def parse_ctype(ty, tymap=nil) return TYPE_INTPTR_T when /\Auintptr_t(?:\s+\w+)?\z/ return TYPE_UINTPTR_T - when "bool" + when /\Abool(?:\s+\w+)?\z/ return TYPE_BOOL when /\*/, /\[[\s\d]*\]/ return TYPE_VOIDP diff --git a/test/fiddle/test_cparser.rb b/test/fiddle/test_cparser.rb index f1b67476..20529115 100644 --- a/test/fiddle/test_cparser.rb +++ b/test/fiddle/test_cparser.rb @@ -277,6 +277,11 @@ def test_struct_string assert_equal [[TYPE_INT,TYPE_VOIDP,TYPE_VOIDP], ['x', 'cb', 'name']], parse_struct_signature('int x; void (*cb)(); const char* name') end + def test_struct_bool + assert_equal([[TYPE_INT, TYPE_BOOL], ['x', 'toggle']], + parse_struct_signature('int x; bool toggle')) + end + def test_struct_undefined assert_raise(DLError) { parse_struct_signature(['int i', 'DWORD cb']) } end