Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package types | 5 package types |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "errors" | 9 "errors" |
| 10 "fmt" | 10 "fmt" |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 // together. | 157 // together. |
| 158 func (s StreamName) Concat(o ...StreamName) StreamName { | 158 func (s StreamName) Concat(o ...StreamName) StreamName { |
| 159 parts := make([]string, len(o)+1) | 159 parts := make([]string, len(o)+1) |
| 160 parts[0] = string(s) | 160 parts[0] = string(s) |
| 161 for i, c := range o { | 161 for i, c := range o { |
| 162 parts[i+1] = string(c) | 162 parts[i+1] = string(c) |
| 163 } | 163 } |
| 164 return StreamName(Construct(parts...)) | 164 return StreamName(Construct(parts...)) |
| 165 } | 165 } |
| 166 | 166 |
| 167 // AsPathPrefix uses s as the prefix component of a StreamPath and constructs | |
| 168 // the remainder of hte path with the supplied name. | |
|
nodir
2016/05/17 02:45:59
s/hte/the
dnj (Google)
2016/05/17 14:44:33
Done.
| |
| 169 // | |
| 170 // If name is empty, the resulting path will end in the path separator. For | |
| 171 // example, if s is "foo/bar" and name is "", the path will be "foo/bar/+". | |
| 172 // | |
| 173 // If name is a valid StreamName, this will construct a valid StreamPath. If s | |
|
nodir
2016/05/17 02:45:59
"If name is a valid StreamName, this will construc
dnj (Google)
2016/05/17 14:44:33
Done.
| |
| 174 // is a valid StreamName, this will construct a valid partial StreamPath. | |
| 175 func (s StreamName) AsPathPrefix(name StreamName) StreamPath { | |
| 176 return StreamPath(Construct(string(s), StreamPathSepStr, string(name))) | |
| 177 } | |
| 178 | |
| 167 // Validate tests whether the stream name is valid. | 179 // Validate tests whether the stream name is valid. |
| 168 func (s StreamName) Validate() error { | 180 func (s StreamName) Validate() error { |
| 169 if len(s) == 0 { | 181 if len(s) == 0 { |
| 170 return errors.New("must contain at least one character") | 182 return errors.New("must contain at least one character") |
| 171 } | 183 } |
| 172 if len(s) > MaxStreamNameLength { | 184 if len(s) > MaxStreamNameLength { |
| 173 return fmt.Errorf("stream name is too long (%d > %d)", len(s), M axStreamNameLength) | 185 return fmt.Errorf("stream name is too long (%d > %d)", len(s), M axStreamNameLength) |
| 174 } | 186 } |
| 175 | 187 |
| 176 var lastRune rune | 188 var lastRune rune |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 // MarshalJSON implements json.Marshaler. | 243 // MarshalJSON implements json.Marshaler. |
| 232 func (s StreamName) MarshalJSON() ([]byte, error) { | 244 func (s StreamName) MarshalJSON() ([]byte, error) { |
| 233 v := string(s) | 245 v := string(s) |
| 234 return json.Marshal(&v) | 246 return json.Marshal(&v) |
| 235 } | 247 } |
| 236 | 248 |
| 237 // A StreamPath consists of two StreamName, joined via a StreamPathSep (+) | 249 // A StreamPath consists of two StreamName, joined via a StreamPathSep (+) |
| 238 // separator. | 250 // separator. |
| 239 type StreamPath string | 251 type StreamPath string |
| 240 | 252 |
| 241 // MakeStreamPath creates a StreamPath by joining prefix and name components. | |
| 242 func MakeStreamPath(prefix, name []StreamName) StreamPath { | |
| 243 o := len(prefix) | |
| 244 sp := make([]string, o+len(name)+1) | |
| 245 for i, v := range prefix { | |
| 246 sp[i] = string(v) | |
| 247 } | |
| 248 sp[o] = StreamPathSepStr | |
| 249 for _, v := range name { | |
| 250 o++ | |
| 251 sp[o] = string(v) | |
| 252 } | |
| 253 return StreamPath(Construct(sp...)) | |
| 254 } | |
| 255 | |
| 256 // Split splits a StreamPath into its prefix and name components. | 253 // Split splits a StreamPath into its prefix and name components. |
| 257 // | 254 // |
| 258 // If there is no divider present (e.g., foo/bar/baz), the result will parse | 255 // If there is no divider present (e.g., foo/bar/baz), the result will parse |
| 259 // as the stream prefix with an empty name component. | 256 // as the stream prefix with an empty name component. |
| 260 func (p StreamPath) Split() (prefix StreamName, name StreamName) { | 257 func (p StreamPath) Split() (prefix StreamName, name StreamName) { |
| 261 prefix, _, name = p.SplitParts() | 258 prefix, _, name = p.SplitParts() |
| 262 return | 259 return |
| 263 } | 260 } |
| 264 | 261 |
| 265 // SplitParts splits a StreamPath into its prefix and name components. | 262 // SplitParts splits a StreamPath into its prefix and name components. |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 | 431 |
| 435 return s | 432 return s |
| 436 } | 433 } |
| 437 | 434 |
| 438 func segmentCount(s string) int { | 435 func segmentCount(s string) int { |
| 439 if len(s) == 0 { | 436 if len(s) == 0 { |
| 440 return 0 | 437 return 0 |
| 441 } | 438 } |
| 442 return strings.Count(s, string(StreamNameSep)) + 1 | 439 return strings.Count(s, string(StreamNameSep)) + 1 |
| 443 } | 440 } |
| OLD | NEW |