name.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. package model
  2. import (
  3. "cmp"
  4. "errors"
  5. "fmt"
  6. "hash/maphash"
  7. "io"
  8. "log/slog"
  9. "path"
  10. "path/filepath"
  11. "slices"
  12. "strings"
  13. "sync"
  14. "github.com/ollama/ollama/types/structs"
  15. )
  16. // Errors
  17. var (
  18. // ErrInvalidName, ErrIncompleteName, and ErrInvalidDigest are not
  19. // used by this package, but are exported so that other packages can
  20. // use them, instead of defining their own errors for them.
  21. ErrInvalidName = errors.New("invalid model name")
  22. ErrIncompleteName = errors.New("incomplete model name")
  23. ErrInvalidDigest = errors.New("invalid digest")
  24. )
  25. // Defaults
  26. const (
  27. // MaskDefault is the default mask used by [Name.DisplayShortest].
  28. MaskDefault = "registry.ollama.ai/library/?:latest"
  29. // MaskNothing is a mask that masks nothing.
  30. MaskNothing = "?/?/?:?"
  31. // DefaultFill is the default fill used by [ParseName].
  32. FillDefault = "registry.ollama.ai/library/?:latest+Q4_0"
  33. // FillNothing is a fill that fills nothing.
  34. FillNothing = "?/?/?:?+?"
  35. )
  36. const MaxNamePartLen = 128
  37. type PartKind int
  38. // Levels of concreteness
  39. const (
  40. // Each value aligns with its index in the Name.parts array.
  41. PartHost PartKind = iota
  42. PartNamespace
  43. PartModel
  44. PartTag
  45. PartBuild
  46. PartDigest
  47. // NumParts is the number of parts in a Name. In this list, it must
  48. // follow the final part.
  49. NumParts
  50. PartExtraneous = -1
  51. )
  52. var kindNames = map[PartKind]string{
  53. PartHost: "Host",
  54. PartNamespace: "Namespace",
  55. PartModel: "Name",
  56. PartTag: "Tag",
  57. PartBuild: "Build",
  58. PartDigest: "Digest",
  59. }
  60. func (k PartKind) String() string {
  61. return cmp.Or(kindNames[k], "Unknown")
  62. }
  63. // Name is an opaque reference to a model. It holds the parts of a model
  64. // with the case preserved, but is not directly comparable with other Names
  65. // since model names can be represented with different casing depending on
  66. // the use case. For instance, "Mistral" and "mistral" are the same model
  67. // but each version may have come from different sources (e.g. copied from a
  68. // Web page, or from a file path).
  69. //
  70. // Valid Names can ONLY be constructed by calling [ParseName].
  71. //
  72. // A Name is valid if and only if is have a valid Model part. The other parts
  73. // are optional.
  74. //
  75. // A Name is considered "complete" if it has all parts present. To check if a
  76. // Name is complete, use [Name.IsComplete].
  77. //
  78. // To compare two names in a case-insensitive manner, use [Name.EqualFold].
  79. //
  80. // The parts of a Name are:
  81. //
  82. // - Host: the domain of the model (optional)
  83. // - Namespace: the namespace of the model (optional)
  84. // - Model: the name of the model (required)
  85. // - Tag: the tag of the model (optional)
  86. // - Build: the build of the model; usually the quantization or "file type" (optional)
  87. //
  88. // The parts can be obtained in their original form by calling [Name.Parts].
  89. //
  90. // To check if a Name has at minimum a valid model part, use [Name.IsValid].
  91. type Name struct {
  92. _ structs.Incomparable
  93. parts [NumParts]string // host, namespace, model, tag, build, digest
  94. // TODO(bmizerany): track offsets and hold s (raw string) here? We
  95. // could pack the offsets all into a single uint64 since the first
  96. // parts take less bits since their max offset is less than the max
  97. // offset of the next part. This would save a ton of bytes per Name
  98. // and mean zero allocations for String.
  99. }
  100. // ParseName parses s into a Name, and returns the result of filling it with
  101. // defaults. The input string must be a valid string
  102. // representation of a model name in the form:
  103. //
  104. // [host/][namespace/]<model>[:tag][+build][@<digest-type>-<digest>]
  105. //
  106. // The name part is required, all others are optional. If a part is missing,
  107. // it is left empty in the returned Name. If a part is invalid, the zero Ref
  108. // value is returned.
  109. //
  110. // The build part is normalized to uppercase.
  111. //
  112. // Examples of valid paths:
  113. //
  114. // "example.com/library/mistral:7b+x"
  115. // "example.com/eva/mistral:7b+Q4_0"
  116. // "mistral:7b+x"
  117. // "example.com/mike/mistral:latest+Q4_0"
  118. // "example.com/bruce/mistral:latest"
  119. // "example.com/pdevine/thisisfine:7b+Q4_0@sha256-1234567890abcdef"
  120. //
  121. // Examples of invalid paths:
  122. //
  123. // "example.com/mistral:7b+"
  124. // "example.com/mistral:7b+Q4_0+"
  125. // "x/y/z/z:8n+I"
  126. // ""
  127. //
  128. // It returns the zero value if any part is invalid.
  129. //
  130. // # Fills
  131. //
  132. // For any valid s, the fill string is used to fill in missing parts of the
  133. // Name. The fill string must be a valid Name with the exception that any part
  134. // may be the string ("?"), which will not be considered for filling.
  135. func ParseName(s, fill string) Name {
  136. var r Name
  137. parts(s)(func(kind PartKind, part string) bool {
  138. if kind == PartDigest && !ParseDigest(part).IsValid() {
  139. r = Name{}
  140. return false
  141. }
  142. if kind == PartExtraneous || !IsValidNamePart(kind, part) {
  143. r = Name{}
  144. return false
  145. }
  146. r.parts[kind] = part
  147. return true
  148. })
  149. if r.IsValid() || r.IsResolved() {
  150. return fillName(r, fill)
  151. }
  152. return Name{}
  153. }
  154. func parseMask(s string) Name {
  155. var r Name
  156. parts(s)(func(kind PartKind, part string) bool {
  157. if part == "?" {
  158. // mask part; treat as empty but valid
  159. return true
  160. }
  161. if !IsValidNamePart(kind, part) {
  162. panic(fmt.Errorf("invalid mask part %s: %q", kind, part))
  163. }
  164. r.parts[kind] = part
  165. return true
  166. })
  167. return r
  168. }
  169. func MustParseName(s, fill string) Name {
  170. r := ParseName(s, fill)
  171. if !r.IsValid() {
  172. panic("invalid Name: " + s)
  173. }
  174. return r
  175. }
  176. // fillName fills in the missing parts of dst with the parts of src.
  177. //
  178. // The returned Name will only be valid if dst is valid.
  179. //
  180. // It skipps fill parts that are "?".
  181. func fillName(r Name, fill string) Name {
  182. fill = cmp.Or(fill, FillDefault)
  183. f := parseMask(fill)
  184. if fill != FillNothing && f.IsZero() {
  185. panic("invalid fill")
  186. }
  187. for i := range r.parts {
  188. if f.parts[i] == "?" {
  189. continue
  190. }
  191. r.parts[i] = cmp.Or(r.parts[i], f.parts[i])
  192. }
  193. return r
  194. }
  195. // WithBuild returns a copy of r with the build set to the given string.
  196. func (r Name) WithBuild(build string) Name {
  197. r.parts[PartBuild] = build
  198. return r
  199. }
  200. func (r Name) WithDigest(digest Digest) Name {
  201. r.parts[PartDigest] = digest.String()
  202. return r
  203. }
  204. var mapHashSeed = maphash.MakeSeed()
  205. // MapHash returns a case insensitive hash for use in maps and equality
  206. // checks. For a convenient way to compare names, use [Name.EqualFold].
  207. //
  208. //nolint:errcheck
  209. func (r Name) MapHash() uint64 {
  210. // correctly hash the parts with case insensitive comparison
  211. var h maphash.Hash
  212. h.SetSeed(mapHashSeed)
  213. for _, part := range r.parts {
  214. // downcase the part for hashing
  215. for i := range part {
  216. c := part[i]
  217. if c >= 'A' && c <= 'Z' {
  218. c = c - 'A' + 'a'
  219. }
  220. h.WriteByte(c)
  221. }
  222. }
  223. return h.Sum64()
  224. }
  225. func (r Name) slice(from, to PartKind) Name {
  226. var v Name
  227. copy(v.parts[from:to+1], r.parts[from:to+1])
  228. return v
  229. }
  230. // DisplayShortest returns the shortest possible, masked display string in form:
  231. //
  232. // [host/][<namespace>/]<model>[:<tag>]
  233. //
  234. // # Masks
  235. //
  236. // The mask is a string that specifies which parts of the name to omit based
  237. // on case-insensitive comparison. [Name.DisplayShortest] omits parts of the name
  238. // that are the same as the mask, moving from left to right until the first
  239. // unequal part is found. It then moves right to left until the first unequal
  240. // part is found. The result is the shortest possible display string.
  241. //
  242. // Unlike a [Name] the mask can contain "?" characters which are treated as
  243. // wildcards. A "?" will never match a part of the name, since a valid name
  244. // can never contain a "?" character.
  245. //
  246. // For example: Given a Name ("registry.ollama.ai/library/mistral:latest") masked
  247. // with ("registry.ollama.ai/library/?:latest") will produce the display string
  248. // ("mistral").
  249. //
  250. // If mask is the empty string, then [MaskDefault] is used.
  251. //
  252. // DisplayShortest panics if the mask is not the empty string, MaskNothing, and
  253. // invalid.
  254. //
  255. // # Builds
  256. //
  257. // For now, DisplayShortest does consider the build or return one in the
  258. // result. We can lift this restriction when needed.
  259. func (r Name) DisplayShortest(mask string) string {
  260. mask = cmp.Or(mask, MaskDefault)
  261. d := parseMask(mask)
  262. if mask != MaskNothing && r.IsZero() {
  263. panic("invalid Name")
  264. }
  265. for i := range PartTag {
  266. if !strings.EqualFold(r.parts[i], d.parts[i]) {
  267. break
  268. }
  269. r.parts[i] = ""
  270. }
  271. for i := PartTag; i >= 0; i-- {
  272. if !strings.EqualFold(r.parts[i], d.parts[i]) {
  273. break
  274. }
  275. r.parts[i] = ""
  276. }
  277. return r.slice(PartHost, PartTag).DisplayLong()
  278. }
  279. // DisplayLongest returns the result of r.DisplayShortest(MaskNothing).
  280. func (r Name) DisplayLongest() string {
  281. return r.DisplayShortest(MaskNothing)
  282. }
  283. var seps = [...]string{
  284. PartHost: "/",
  285. PartNamespace: "/",
  286. PartModel: ":",
  287. PartTag: "+",
  288. PartBuild: "@",
  289. PartDigest: "",
  290. }
  291. // WriteTo implements io.WriterTo. It writes the fullest possible display
  292. // string in form:
  293. //
  294. // <host>/<namespace>/<model>:<tag>+<build>@<digest-type>-<digest>
  295. //
  296. // Missing parts and their separators are not written.
  297. //
  298. // The full digest is always prefixed with "@". That is if [Name.IsValid]
  299. // reports false and [Name.IsResolved] reports true, then the string is
  300. // returned as "@<digest-type>-<digest>".
  301. func (r Name) writeTo(w io.StringWriter) error {
  302. var partsWritten int
  303. for i := range r.parts {
  304. if r.parts[i] == "" {
  305. continue
  306. }
  307. if partsWritten > 0 || i == int(PartDigest) {
  308. if _, err := w.WriteString(seps[i-1]); err != nil {
  309. return err
  310. }
  311. }
  312. if _, err := w.WriteString(r.parts[i]); err != nil {
  313. return err
  314. }
  315. partsWritten++
  316. }
  317. return nil
  318. }
  319. var builderPool = sync.Pool{
  320. New: func() interface{} {
  321. return &strings.Builder{}
  322. },
  323. }
  324. // DisplayLong returns the fullest possible display string in form:
  325. //
  326. // <host>/<namespace>/<model>:<tag>+<build>
  327. //
  328. // If any part is missing, it is omitted from the display string.
  329. func (r Name) DisplayLong() string {
  330. b := builderPool.Get().(*strings.Builder)
  331. defer builderPool.Put(b)
  332. b.Reset()
  333. b.Grow(50) // arbitrarily long enough for most names
  334. _ = r.writeTo(b)
  335. return b.String()
  336. }
  337. // GoString implements fmt.GoStringer. It returns a string suitable for
  338. // debugging and logging. It is similar to [Name.DisplayLong] but it always
  339. // returns a string that includes all parts of the Name, with missing parts
  340. // replaced with a ("?").
  341. func (r Name) GoString() string {
  342. for i := range r.parts {
  343. r.parts[i] = cmp.Or(r.parts[i], "?")
  344. }
  345. return r.DisplayLong()
  346. }
  347. // LogValue implements slog.Valuer.
  348. func (r Name) LogValue() slog.Value {
  349. return slog.StringValue(r.GoString())
  350. }
  351. // IsComplete reports whether the Name is fully qualified. That is it has a
  352. // domain, namespace, name, tag, and build.
  353. func (r Name) IsComplete() bool {
  354. return !slices.Contains(r.parts[:PartDigest], "")
  355. }
  356. // IsCompleteNoBuild is like [Name.IsComplete] but it does not require the
  357. // build part to be present.
  358. func (r Name) IsCompleteNoBuild() bool {
  359. return !slices.Contains(r.parts[:PartBuild], "")
  360. }
  361. // IsResolved reports true if the Name has a valid digest.
  362. //
  363. // It is possible to have a valid Name, or a complete Name that is not
  364. // resolved.
  365. func (r Name) IsResolved() bool {
  366. return r.Digest().IsValid()
  367. }
  368. // Digest returns the digest part of the Name, if any.
  369. //
  370. // If Digest returns a non-empty string, then [Name.IsResolved] will return
  371. // true, and digest is considered valid.
  372. func (r Name) Digest() Digest {
  373. // This was already validated by ParseName, so we can just return it.
  374. return Digest{r.parts[PartDigest]}
  375. }
  376. // EqualFold reports whether r and o are equivalent model names, ignoring
  377. // case.
  378. func (r Name) EqualFold(o Name) bool {
  379. return r.CompareFold(o) == 0
  380. }
  381. // CompareFold performs a case-insensitive cmp.Compare on r and o.
  382. //
  383. // This can be used with [slices.SortFunc].
  384. //
  385. // For simple equality checks, use [Name.EqualFold].
  386. func (r Name) CompareFold(o Name) int {
  387. return slices.CompareFunc(r.parts[:], o.parts[:], compareFold)
  388. }
  389. func compareFold(a, b string) int {
  390. return slices.CompareFunc([]rune(a), []rune(b), func(a, b rune) int {
  391. return cmp.Compare(downcase(a), downcase(b))
  392. })
  393. }
  394. func downcase(r rune) rune {
  395. if r >= 'A' && r <= 'Z' {
  396. return r - 'A' + 'a'
  397. }
  398. return r
  399. }
  400. func (r Name) Host() string { return r.parts[PartHost] }
  401. func (r Name) Namespace() string { return r.parts[PartNamespace] }
  402. func (r Name) Model() string { return r.parts[PartModel] }
  403. func (r Name) Build() string { return r.parts[PartBuild] }
  404. func (r Name) Tag() string { return r.parts[PartTag] }
  405. // iter_Seq2 is a iter.Seq2 defined here to avoid the current build
  406. // restrictions in the go1.22 iter package requiring the
  407. // goexperiment.rangefunc tag to be set via the GOEXPERIMENT=rangefunc flag,
  408. // which we are not yet ready to support.
  409. //
  410. // Once we are ready to support rangefunc, this can be removed and replaced
  411. // with the iter.Seq2 type.
  412. type iter_Seq2[A, B any] func(func(A, B) bool)
  413. // Parts returns a sequence of the parts of a Name string from most specific
  414. // to least specific.
  415. //
  416. // It normalizes the input string by removing "http://" and "https://" only.
  417. // No other normalizations are performed.
  418. func parts(s string) iter_Seq2[PartKind, string] {
  419. return func(yield func(PartKind, string) bool) {
  420. if strings.HasPrefix(s, "http://") {
  421. s = strings.TrimPrefix(s, "http://")
  422. } else {
  423. s = strings.TrimPrefix(s, "https://")
  424. }
  425. if len(s) > MaxNamePartLen || len(s) == 0 {
  426. return
  427. }
  428. numConsecutiveDots := 0
  429. partLen := 0
  430. state, j := PartDigest, len(s)
  431. for i := len(s) - 1; i >= 0; i-- {
  432. if partLen++; partLen > MaxNamePartLen {
  433. // catch a part that is too long early, so
  434. // we don't keep spinning on it, waiting for
  435. // an isInValidPart check which would scan
  436. // over it again.
  437. yield(state, s[i+1:j])
  438. return
  439. }
  440. switch s[i] {
  441. case '@':
  442. switch state {
  443. case PartDigest:
  444. if !yield(PartDigest, s[i+1:j]) {
  445. return
  446. }
  447. if i == 0 {
  448. // This is the form
  449. // "@<digest>" which is valid.
  450. //
  451. // We're done.
  452. return
  453. }
  454. state, j, partLen = PartBuild, i, 0
  455. default:
  456. yield(PartExtraneous, s[i+1:j])
  457. return
  458. }
  459. case '+':
  460. switch state {
  461. case PartBuild, PartDigest:
  462. if !yield(PartBuild, s[i+1:j]) {
  463. return
  464. }
  465. state, j, partLen = PartTag, i, 0
  466. default:
  467. yield(PartExtraneous, s[i+1:j])
  468. return
  469. }
  470. case ':':
  471. switch state {
  472. case PartTag, PartBuild, PartDigest:
  473. if !yield(PartTag, s[i+1:j]) {
  474. return
  475. }
  476. state, j, partLen = PartModel, i, 0
  477. case PartHost:
  478. // noop: support for host:port
  479. default:
  480. yield(PartExtraneous, s[i+1:j])
  481. return
  482. }
  483. case '/':
  484. switch state {
  485. case PartModel, PartTag, PartBuild, PartDigest:
  486. if !yield(PartModel, s[i+1:j]) {
  487. return
  488. }
  489. state, j = PartNamespace, i
  490. case PartNamespace:
  491. if !yield(PartNamespace, s[i+1:j]) {
  492. return
  493. }
  494. state, j, partLen = PartHost, i, 0
  495. default:
  496. yield(PartExtraneous, s[i+1:j])
  497. return
  498. }
  499. default:
  500. if s[i] == '.' {
  501. if numConsecutiveDots++; numConsecutiveDots > 1 {
  502. yield(state, "")
  503. return
  504. }
  505. } else {
  506. numConsecutiveDots = 0
  507. }
  508. }
  509. }
  510. if state <= PartNamespace {
  511. yield(state, s[:j])
  512. } else {
  513. yield(PartModel, s[:j])
  514. }
  515. }
  516. }
  517. func (r Name) IsZero() bool {
  518. return r.parts == [NumParts]string{}
  519. }
  520. // IsValid reports if a model has at minimum a valid model part.
  521. func (r Name) IsValid() bool {
  522. // Parts ensures we only have valid parts, so no need to validate
  523. // them here, only check if we have a name or not.
  524. return r.parts[PartModel] != ""
  525. }
  526. // ParseNameFromURLPath parses forms of a URL path into a Name. Specifically,
  527. // it trims any leading "/" and then calls [ParseName] with fill.
  528. func ParseNameFromURLPath(s, fill string) Name {
  529. s = strings.TrimPrefix(s, "/")
  530. return ParseName(s, fill)
  531. }
  532. // URLPath returns a complete, canonicalized, relative URL path using the parts of a
  533. // complete Name.
  534. //
  535. // The parts maintain their original case.
  536. //
  537. // Example:
  538. //
  539. // ParseName("example.com/namespace/model:tag+build").URLPath() // returns "/example.com/namespace/model:tag"
  540. func (r Name) DisplayURLPath() string {
  541. return r.DisplayShortest(MaskNothing)
  542. }
  543. // URLPath returns a complete, canonicalized, relative URL path using the parts of a
  544. // complete Name in the form:
  545. //
  546. // <host>/<namespace>/<model>/<tag>
  547. //
  548. // The parts are downcased.
  549. func (r Name) URLPath() string {
  550. return strings.ToLower(path.Join(r.parts[:PartBuild]...))
  551. }
  552. // ParseNameFromFilepath parses a file path into a Name. The input string must be a
  553. // valid file path representation of a model name in the form:
  554. //
  555. // host/namespace/model/tag/build
  556. //
  557. // The zero valid is returned if s does not contain all path elements
  558. // leading up to the model part, or if any path element is an invalid part
  559. // for the its corresponding part kind.
  560. //
  561. // The fill string is used to fill in missing parts of any constructed Name.
  562. // See [ParseName] for more information on the fill string.
  563. func ParseNameFromFilepath(s, fill string) Name {
  564. var r Name
  565. for i := range PartBuild + 1 {
  566. part, rest, _ := strings.Cut(s, string(filepath.Separator))
  567. if !IsValidNamePart(i, part) {
  568. return Name{}
  569. }
  570. r.parts[i] = part
  571. s = rest
  572. if s == "" {
  573. break
  574. }
  575. }
  576. if s != "" {
  577. return Name{}
  578. }
  579. if !r.IsValid() {
  580. return Name{}
  581. }
  582. return fillName(r, fill)
  583. }
  584. // Filepath returns a complete, canonicalized, relative file path using the
  585. // parts of a complete Name.
  586. //
  587. // Each parts is downcased, except for the build part which is upcased.
  588. //
  589. // Example:
  590. //
  591. // ParseName("example.com/namespace/model:tag+build").Filepath() // returns "example.com/namespace/model/tag/BUILD"
  592. func (r Name) Filepath() string {
  593. for i := range r.parts {
  594. if PartKind(i) == PartBuild {
  595. r.parts[i] = strings.ToUpper(r.parts[i])
  596. } else {
  597. r.parts[i] = strings.ToLower(r.parts[i])
  598. }
  599. }
  600. return filepath.Join(r.parts[:]...)
  601. }
  602. // FilepathNoBuild returns a complete, canonicalized, relative file path using
  603. // the parts of a complete Name, but without the build part.
  604. func (r Name) FilepathNoBuild() string {
  605. for i := range PartBuild {
  606. r.parts[i] = strings.ToLower(r.parts[i])
  607. }
  608. return filepath.Join(r.parts[:PartBuild]...)
  609. }
  610. // IsValidNamePart reports if s contains all valid characters for the given
  611. // part kind and is under MaxNamePartLen bytes.
  612. func IsValidNamePart(kind PartKind, s string) bool {
  613. if len(s) > MaxNamePartLen {
  614. return false
  615. }
  616. if s == "" {
  617. return false
  618. }
  619. var consecutiveDots int
  620. for _, c := range []byte(s) {
  621. if c == '.' {
  622. if consecutiveDots++; consecutiveDots >= 2 {
  623. return false
  624. }
  625. } else {
  626. consecutiveDots = 0
  627. }
  628. if !isValidByteFor(kind, c) {
  629. return false
  630. }
  631. }
  632. return true
  633. }
  634. func isValidByteFor(kind PartKind, c byte) bool {
  635. if kind == PartNamespace && c == '.' {
  636. return false
  637. }
  638. if kind == PartHost && c == ':' {
  639. return true
  640. }
  641. if c == '.' || c == '-' {
  642. return true
  643. }
  644. if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' {
  645. return true
  646. }
  647. return false
  648. }