string: Add length() method
While it was possible to access to the last elements of a string via negative indexing, it was not possible easily iterate through it or to check nicely whether it was empty or of an expected size (without comparisons with another expected string).
Also it was not possible to iterate through it, now with the range()
method it would be possible to go char-by-char, but strings doesn't
expose their length so add a method to fill this gap.
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 66.16%. Comparing base (
dac212e) to head (60b73cd). Report is 2477 commits behind head on master.
Additional details and impacted files
@@ Coverage Diff @@
## master #10177 +/- ##
==========================================
+ Coverage 66.14% 66.16% +0.02%
==========================================
Files 406 406
Lines 86634 86641 +7
Branches 19161 19155 -6
==========================================
+ Hits 57305 57330 +25
+ Misses 24897 24886 -11
+ Partials 4432 4425 -7
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
As a matter of curiosity, what would you use this for?
e.g. you mention checking whether a string is an "expected" length, but when does the length matter and the contents don't?
The natural use cases of len() in python that I can think of offhand, tend to be of the non-string variety -- or else for example when one wants to use the length of the string for calculating padding e.g. when writing files or printing output.
Seems natural to have for API completeness IMHO, and implementation looks good. Just missing a release notes snippet, add a .md file in doc/markdown/snippets.
As a matter of curiosity, what would you use this for?
Well, meson is getting quite flexible now and it allows to do lots of string manipulations, that can be even more complex when you use fs.read(), where you may want to go through the string and do manipulations that are almost impossible to do without being able to know what are your limits.
Well, meson is getting quite flexible now and it allows to do lots of string manipulations, that can be even more complex when you use fs.read()
As a rule of thumb, whenever you find yourself doing "coding" in your Meson script, the recommended approach is to not do that and instead write a Python script that has the actual logic and just call into that. Parsing the contents of files is very strongly discouraged.
What specific thing are you doing and is there some higher level functionality we could add instead to avoid doing bitbanging inside Meson files?
In my case it's not related to coding, even though it could be, but I had cases in which I wanted go through a string (path) to do manipulations that were not possible using Fs methods (like searching for the common prefix of two of them).
And in general be able to iterate through them, thus the addition that I found to be relevant in many other cases that I couldn't recall now.
Just missing a release notes snippet, add a .md file in doc/markdown/snippets.
Done.
but I had cases in which I wanted go through a string (path) to do manipulations that were not possible using Fs methods (like searching for the common prefix of two of them)
So for this particular case what you actually needed was a function for computing the common prefix? Anything else?
There's even an open PR somewhere for exactly that!