X Tutup
Skip to content

Add has_extension() method to String#109433

Merged
Repiteo merged 1 commit intogodotengine:masterfrom
KoBeWi:hastension
Oct 31, 2025
Merged

Add has_extension() method to String#109433
Repiteo merged 1 commit intogodotengine:masterfrom
KoBeWi:hastension

Conversation

@KoBeWi
Copy link
Member

@KoBeWi KoBeWi commented Aug 8, 2025

I noticed we have lots of code like path.get_extension().to_lower() == "ext". The to_lower() part isn't used consistently (while extensions should always be case insensitive), and the 2 calls will make 2 temporary strings, which while doesn't not really matter, is rather bad.

This PR adds a has_extension() method (not exposed) that does case-insensitive check of file's extension. I went over all usages of get_extension() and applied the new method where applicable.

@KoBeWi KoBeWi added this to the 4.x milestone Aug 8, 2025
@KoBeWi KoBeWi requested review from a team as code owners August 8, 2025 16:02
@KoBeWi KoBeWi requested review from a team August 8, 2025 16:02
@KoBeWi KoBeWi requested review from a team as code owners August 8, 2025 16:02
@KoBeWi KoBeWi removed request for a team August 8, 2025 19:50
@KoBeWi KoBeWi removed request for a team August 8, 2025 19:50
@KoBeWi KoBeWi force-pushed the hastension branch 2 times, most recently from ff83d70 to c8058e7 Compare August 9, 2025 11:27
Copy link
Member

@Ivorforce Ivorforce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure adding this is worth it without profiling evidence. This is especially true because we're once again adding two implementations (one for String and one for const char *, doubling the LOC added.
While I think semantically wrapping operations like this is nice, it's possible to implement it just with the current unoptimized version (lower() == p_ext). We can always optimize if it bottlenecks us anywhere.

@KoBeWi
Copy link
Member Author

KoBeWi commented Aug 12, 2025

Pushed the simpler version. I did a simple benchmark and it's 2.3 times slower, but yeah, it's unlikely to be a bottleneck, so whatever.

Copy link
Member

@AThousandShips AThousandShips left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense as a useful shorthand, minor details only

Copy link
Member

@Ivorforce Ivorforce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@Ivorforce Ivorforce modified the milestones: 4.x, 4.6 Aug 12, 2025
Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me.

@Repiteo Repiteo merged commit ae97321 into godotengine:master Oct 31, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Oct 31, 2025

Thanks!

@KoBeWi KoBeWi deleted the hastension branch October 31, 2025 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

X Tutup