@doc """### lcount(s::AbstractString, char_::Char=' ')
Return `number` of leading `char` in `string`.
""" ->
function lcount(s::AbstractString, char_::Char=' ')
i = start(s)
while !done(s,i)
c, j = next(s,i)
if c != char_
return i - 1
end
i = j
end
return i - 1
end
Working with text/text files one might need more than once to get the number of leading char (e.g. space - text indentation)
my one-liner solution:
first(search(" asdf",r"[^ ]|$")) ÷ sizeof(" ") - 1
(note the OP solution returns the wrong answer for multibyte characters, since it is missing the div by the size of the character representation in the original string type. or preferably just keep count of the characters, rather than returning the final byte offset.)
I suspect that most languages don’t have this because (a) lstrip is more generally useful, because it is generally doing what you wanted, and potentially avoids lots of slicing later (b) lstrip isn’t actually much of a contributor to the program speed for most real-world tasks (e.g. reading a file of the hard drive is typically assumed to be several orders of magnitude slower than copying a string), and if it was, we could just change it to return a SubString
const text = """0 Indent
1 Indent
2 Indent
0 Indent
10 Indent
6 Indent
2 Indent
0 Indent
1 Indent
4 Indent
1 Tab indent
8 Indent"""
Just the cost of creating the potentially large object just to count it's length, and then throwing it away...
In most things I do its also more common to want leading whitespace, ie a class of characters, not a single char. So the OP doesn't add much in my case. Also as pointed out above, the OP returns a code point count for UTF*strings, so its no use for indexing to find the non-leading char anyway.