meson icon indicating copy to clipboard operation
meson copied to clipboard

string: Add length() method

Open 3v1n0 opened this issue 3 years ago • 9 comments

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.

3v1n0 avatar Mar 24 '22 12:03 3v1n0

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.

codecov[bot] avatar Mar 24 '22 13:03 codecov[bot]

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.

eli-schwartz avatar Mar 24 '22 13:03 eli-schwartz

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.

xclaesse avatar Mar 24 '22 14:03 xclaesse

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.

3v1n0 avatar Mar 24 '22 14:03 3v1n0

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?

jpakkane avatar Mar 24 '22 15:03 jpakkane

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.

3v1n0 avatar Mar 24 '22 15:03 3v1n0

Just missing a release notes snippet, add a .md file in doc/markdown/snippets.

Done.

3v1n0 avatar Mar 24 '22 15:03 3v1n0

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?

jpakkane avatar Mar 24 '22 16:03 jpakkane

There's even an open PR somewhere for exactly that!

eli-schwartz avatar Mar 24 '22 16:03 eli-schwartz