logoalt Hacker News

djoldmantoday at 1:27 AM2 repliesview on HN

> _canonicalize_table = str.maketrans( "ABCDEFGHIJKLMNOPQRSTUVWXYZ_.", "abcdefghijklmnopqrstuvwxyz--", )

> ...

> value = name.translate(_canonicalize_table)

> while "--" in value:

> value = value.replace("--", "-")

translate can be wildly fast compared to some commonly used regexes or replacements.


Replies

esttoday at 2:31 AM

I am curious, why not .lower().translate('_.', '--')

show 1 reply
teaearlgraycoldtoday at 1:54 AM

I would expect however that a regex replacement would be much faster than your N^2 while loop.

show 1 reply