Skip to content

Commit 682dc7e

Browse files
authored
Merge pull request #219 from CastXML/string-macos
type-traits: fix string equivalences
2 parents aa563e6 + 0698c3c commit 682dc7e

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ docs = [
6464
examples = [
6565
"notebook",
6666
]
67+
[tool.pytest.ini_options]
68+
pythonpath = [
69+
"src"]

src/pygccxml/declarations/container_traits.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,17 @@ def normalize(self, type_str):
2929
return type_str.replace(' ', '')
3030

3131
def replace_basic_string(self, cls_name):
32-
33-
# Take the lists of all possible string variations
34-
# and clean them up by replacing ::std by std.
35-
str_eq = [
36-
v.replace("::std", "std") for v in
37-
type_traits.string_equivalences]
38-
wstr_eq = [
39-
v.replace("::std", "std") for v in
40-
type_traits.wstring_equivalences]
41-
4232
# Replace all the variations of strings by the smallest one.
4333
strings = {
44-
"std::string": [v for v in str_eq if not v == "std::string"],
45-
"std::wstring": [v for v in wstr_eq if not v == "std::wstring"]}
34+
"std::string":
35+
[v for v in
36+
type_traits.normalized_string_equivalences
37+
if not v == "std::string"],
38+
"std::wstring":
39+
[v for v in
40+
type_traits.normalized_wstring_equivalences
41+
if not v == "std::wstring"]
42+
}
4643

4744
new_name = cls_name
4845
for short_name, long_names in strings.items():

src/pygccxml/declarations/type_traits.py

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -480,40 +480,68 @@ def is_fundamental(type_):
480480
(cpptypes.volatile_t, cpptypes.const_t))
481481

482482

483+
def _normalize(string):
484+
return string.replace(' ', '').replace("::std", "std")
485+
486+
487+
def _normalize_equivalences(equivalences):
488+
return [_normalize(eq) for eq in equivalences]
489+
490+
483491
string_equivalences = [
484492
(
485-
'::std::basic_string<char,std::char_traits<char>,'
486-
'std::allocator<char>>'),
487-
'::std::basic_string<char>', '::std::string']
493+
'::std::basic_string<char, std::char_traits<char>, '
494+
'std::allocator<char>>'
495+
),
496+
'::std::basic_string<char>',
497+
'::std::string'
498+
]
488499

489500
wstring_equivalences = [
490501
(
491-
'::std::basic_string<wchar_t,std::char_traits<wchar_t>,' +
492-
'std::allocator<wchar_t>>'),
493-
'::std::basic_string<wchar_t>', '::std::wstring']
502+
'::std::basic_string<wchar_t, std::char_traits<wchar_t>, '
503+
'std::allocator<wchar_t>>'
504+
),
505+
'::std::basic_string<wchar_t>',
506+
'::std::wstring'
507+
]
494508

495509
ostream_equivalences = [
496-
'::std::basic_ostream<char,std::char_traits<char>>',
510+
'::std::basic_ostream<char, std::char_traits<char>>',
497511
'::std::basic_ostream<char>', '::std::ostream']
498512

499513
wostream_equivalences = [
500-
'::std::basic_ostream<wchar_t,std::char_traits<wchar_t>>',
514+
'::std::basic_ostream<wchar_t, std::char_traits<wchar_t>>',
501515
'::std::basic_ostream<wchar_t>', '::std::wostream']
502516

503517

518+
normalized_string_equivalences = _normalize_equivalences(
519+
string_equivalences
520+
)
521+
normalized_wstring_equivalences = _normalize_equivalences(
522+
wstring_equivalences
523+
)
524+
normalized_ostream_equivalences = _normalize_equivalences(
525+
ostream_equivalences
526+
)
527+
normalized_wostream_equivalences = _normalize_equivalences(
528+
wostream_equivalences
529+
)
530+
531+
504532
def is_std_string(type_):
505533
"""
506534
Returns True, if type represents C++ `std::string`, False otherwise.
507535
508536
"""
509537

510538
if isinstance(type_, str):
511-
return type_ in string_equivalences
539+
return _normalize(type_) in normalized_string_equivalences
512540

513541
type_ = remove_alias(type_)
514542
type_ = remove_reference(type_)
515543
type_ = remove_cv(type_)
516-
return type_.decl_string.replace(' ', '') in string_equivalences
544+
return _normalize(type_.decl_string) in normalized_string_equivalences
517545

518546

519547
def is_std_wstring(type_):
@@ -523,12 +551,12 @@ def is_std_wstring(type_):
523551
"""
524552

525553
if isinstance(type_, str):
526-
return type_ in wstring_equivalences
554+
return _normalize(type_) in normalized_wstring_equivalences
527555

528556
type_ = remove_alias(type_)
529557
type_ = remove_reference(type_)
530558
type_ = remove_cv(type_)
531-
return type_.decl_string.replace(' ', '') in wstring_equivalences
559+
return _normalize(type_.decl_string) in normalized_wstring_equivalences
532560

533561

534562
def is_std_ostream(type_):
@@ -538,12 +566,12 @@ def is_std_ostream(type_):
538566
"""
539567

540568
if isinstance(type_, str):
541-
return type_ in ostream_equivalences
569+
return _normalize(type_) in normalized_ostream_equivalences
542570

543571
type_ = remove_alias(type_)
544572
type_ = remove_reference(type_)
545573
type_ = remove_cv(type_)
546-
return type_.decl_string.replace(' ', '') in ostream_equivalences
574+
return _normalize(type_.decl_string) in normalized_ostream_equivalences
547575

548576

549577
def is_std_wostream(type_):
@@ -553,9 +581,9 @@ def is_std_wostream(type_):
553581
"""
554582

555583
if isinstance(type_, str):
556-
return type_ in wostream_equivalences
584+
return _normalize(type_) in normalized_wostream_equivalences
557585

558586
type_ = remove_alias(type_)
559587
type_ = remove_reference(type_)
560588
type_ = remove_cv(type_)
561-
return type_.decl_string.replace(' ', '') in wostream_equivalences
589+
return _normalize(type_.decl_string) in normalized_wostream_equivalences

0 commit comments

Comments
 (0)