Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type-traits: fix string equivalences #219

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ docs = [
examples = [
"notebook",
]
[tool.pytest.ini_options]
pythonpath = [
"src"]
21 changes: 9 additions & 12 deletions src/pygccxml/declarations/container_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ def normalize(self, type_str):
return type_str.replace(' ', '')

def replace_basic_string(self, cls_name):

# Take the lists of all possible string variations
# and clean them up by replacing ::std by std.
str_eq = [
v.replace("::std", "std") for v in
type_traits.string_equivalences]
wstr_eq = [
v.replace("::std", "std") for v in
type_traits.wstring_equivalences]

# Replace all the variations of strings by the smallest one.
strings = {
"std::string": [v for v in str_eq if not v == "std::string"],
"std::wstring": [v for v in wstr_eq if not v == "std::wstring"]}
"std::string":
[v for v in
type_traits.normalized_string_equivalences
if not v == "std::string"],
"std::wstring":
[v for v in
type_traits.normalized_wstring_equivalences
if not v == "std::wstring"]
}

new_name = cls_name
for short_name, long_names in strings.items():
Expand Down
60 changes: 44 additions & 16 deletions src/pygccxml/declarations/type_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,40 +480,68 @@ def is_fundamental(type_):
(cpptypes.volatile_t, cpptypes.const_t))


def _normalize(string):
return string.replace(' ', '').replace("::std", "std")


def _normalize_equivalences(equivalences):
return [_normalize(eq) for eq in equivalences]


string_equivalences = [
(
'::std::basic_string<char,std::char_traits<char>,'
'std::allocator<char>>'),
'::std::basic_string<char>', '::std::string']
'::std::basic_string<char, std::char_traits<char>, '
'std::allocator<char>>'
),
'::std::basic_string<char>',
'::std::string'
]

wstring_equivalences = [
(
'::std::basic_string<wchar_t,std::char_traits<wchar_t>,' +
'std::allocator<wchar_t>>'),
'::std::basic_string<wchar_t>', '::std::wstring']
'::std::basic_string<wchar_t, std::char_traits<wchar_t>, '
'std::allocator<wchar_t>>'
),
'::std::basic_string<wchar_t>',
'::std::wstring'
]

ostream_equivalences = [
'::std::basic_ostream<char,std::char_traits<char>>',
'::std::basic_ostream<char, std::char_traits<char>>',
'::std::basic_ostream<char>', '::std::ostream']

wostream_equivalences = [
'::std::basic_ostream<wchar_t,std::char_traits<wchar_t>>',
'::std::basic_ostream<wchar_t, std::char_traits<wchar_t>>',
'::std::basic_ostream<wchar_t>', '::std::wostream']


normalized_string_equivalences = _normalize_equivalences(
string_equivalences
)
normalized_wstring_equivalences = _normalize_equivalences(
wstring_equivalences
)
normalized_ostream_equivalences = _normalize_equivalences(
ostream_equivalences
)
normalized_wostream_equivalences = _normalize_equivalences(
wostream_equivalences
)


def is_std_string(type_):
"""
Returns True, if type represents C++ `std::string`, False otherwise.

"""

if isinstance(type_, str):
return type_ in string_equivalences
return _normalize(type_) in normalized_string_equivalences

type_ = remove_alias(type_)
type_ = remove_reference(type_)
type_ = remove_cv(type_)
return type_.decl_string.replace(' ', '') in string_equivalences
return _normalize(type_.decl_string) in normalized_string_equivalences


def is_std_wstring(type_):
Expand All @@ -523,12 +551,12 @@ def is_std_wstring(type_):
"""

if isinstance(type_, str):
return type_ in wstring_equivalences
return _normalize(type_) in normalized_wstring_equivalences

type_ = remove_alias(type_)
type_ = remove_reference(type_)
type_ = remove_cv(type_)
return type_.decl_string.replace(' ', '') in wstring_equivalences
return _normalize(type_.decl_string) in normalized_wstring_equivalences


def is_std_ostream(type_):
Expand All @@ -538,12 +566,12 @@ def is_std_ostream(type_):
"""

if isinstance(type_, str):
return type_ in ostream_equivalences
return _normalize(type_) in normalized_ostream_equivalences

type_ = remove_alias(type_)
type_ = remove_reference(type_)
type_ = remove_cv(type_)
return type_.decl_string.replace(' ', '') in ostream_equivalences
return _normalize(type_.decl_string) in normalized_ostream_equivalences


def is_std_wostream(type_):
Expand All @@ -553,9 +581,9 @@ def is_std_wostream(type_):
"""

if isinstance(type_, str):
return type_ in wostream_equivalences
return _normalize(type_) in normalized_wostream_equivalences

type_ = remove_alias(type_)
type_ = remove_reference(type_)
type_ = remove_cv(type_)
return type_.decl_string.replace(' ', '') in wostream_equivalences
return _normalize(type_.decl_string) in normalized_wostream_equivalences
Loading