Suppose I have a workflow that involves examining the start of a long string (LS, say) to see whether it begins with a shorter string SS. If it does, I chop off the matching part of LS and do something with the remaining part. Otherwise, I do something else. (The specific case that prompted this question was a parsing library.)
def do_thing(LS, SS):
if (LS.startswith(SS)):
action_on_match(LS[len(SS):])
else:
action_on_no_match()
This is straightforward. Now, though, suppose that I want to do the same thing but this time I want the strings to be matched case-insensitively. It is possible to test whether "LS.startswith(SS) but case-insensitively". But how should I determine how much of LS to "chop off" when I pass it in to action_on_match()? It isn't sufficient to just use len(SS) as it was before, because if I'm uppercasing or lowercasing or casefolding things, then the length of the matching prefix of LS might not be what I expect: changing the case of a string can change its length. It is important that the part of LS passed to action_on_match() be exactly what the program received as input (after the cutoff point, of course).
Aucun commentaire:
Enregistrer un commentaire