This process is way more complex than you might think. The difficulty is that the host name is derived from the computer name and the computer name can contain arbitrary characters, including characters from non-Latin scripts.
Try this: Set you computer name to
トトロ
[1]. When you do this, the local hostname changes to
totoro.local
. That’s is very cool. And it also applies across languages. A computer name of U+967d U+5149 [2] translates to a local hostname of
yangguang.local
, and so on.
The actual algorithm is not documented and, AFAIK, there’s no API access to it. However, the underlying primitives are available [3]. Specifically:
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
[1] The name of the main character from My Neighbor Totoro.
[2] One of the pandas at Edinburgh zoo. DevForums will not, alas, let me post the Han characters.
[3] While it uses CF APIs for this, most of these APIs have NS, and hence Swift-friendly, equivalents.
[4]
CFStringTransform
supports arbitrary ICU string transforms. The transform used currently is
Latin; Latin-ASCII; [^-._a-z0-9A-Z] Remove
, although that has changed in the past and it’s not hard to image it changing again in the future.