| 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 the path with the supplied name. |
| 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 both s and name are valid StreamNames, this will construct a valid |
| 174 // StreamPath. If s is a valid StreamName and name is empty, this will construct |
| 175 // a valid partial StreamPath. |
| 176 func (s StreamName) AsPathPrefix(name StreamName) StreamPath { |
| 177 return StreamPath(Construct(string(s), StreamPathSepStr, string(name))) |
| 178 } |
| 179 |
| 167 // Validate tests whether the stream name is valid. | 180 // Validate tests whether the stream name is valid. |
| 168 func (s StreamName) Validate() error { | 181 func (s StreamName) Validate() error { |
| 169 if len(s) == 0 { | 182 if len(s) == 0 { |
| 170 return errors.New("must contain at least one character") | 183 return errors.New("must contain at least one character") |
| 171 } | 184 } |
| 172 if len(s) > MaxStreamNameLength { | 185 if len(s) > MaxStreamNameLength { |
| 173 return fmt.Errorf("stream name is too long (%d > %d)", len(s), M
axStreamNameLength) | 186 return fmt.Errorf("stream name is too long (%d > %d)", len(s), M
axStreamNameLength) |
| 174 } | 187 } |
| 175 | 188 |
| 176 var lastRune rune | 189 var lastRune rune |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 // MarshalJSON implements json.Marshaler. | 244 // MarshalJSON implements json.Marshaler. |
| 232 func (s StreamName) MarshalJSON() ([]byte, error) { | 245 func (s StreamName) MarshalJSON() ([]byte, error) { |
| 233 v := string(s) | 246 v := string(s) |
| 234 return json.Marshal(&v) | 247 return json.Marshal(&v) |
| 235 } | 248 } |
| 236 | 249 |
| 237 // A StreamPath consists of two StreamName, joined via a StreamPathSep (+) | 250 // A StreamPath consists of two StreamName, joined via a StreamPathSep (+) |
| 238 // separator. | 251 // separator. |
| 239 type StreamPath string | 252 type StreamPath string |
| 240 | 253 |
| 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. | 254 // Split splits a StreamPath into its prefix and name components. |
| 257 // | 255 // |
| 258 // If there is no divider present (e.g., foo/bar/baz), the result will parse | 256 // 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. | 257 // as the stream prefix with an empty name component. |
| 260 func (p StreamPath) Split() (prefix StreamName, name StreamName) { | 258 func (p StreamPath) Split() (prefix StreamName, name StreamName) { |
| 261 prefix, _, name = p.SplitParts() | 259 prefix, _, name = p.SplitParts() |
| 262 return | 260 return |
| 263 } | 261 } |
| 264 | 262 |
| 265 // SplitParts splits a StreamPath into its prefix and name components. | 263 // SplitParts splits a StreamPath into its prefix and name components. |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 | 432 |
| 435 return s | 433 return s |
| 436 } | 434 } |
| 437 | 435 |
| 438 func segmentCount(s string) int { | 436 func segmentCount(s string) int { |
| 439 if len(s) == 0 { | 437 if len(s) == 0 { |
| 440 return 0 | 438 return 0 |
| 441 } | 439 } |
| 442 return strings.Count(s, string(StreamNameSep)) + 1 | 440 return strings.Count(s, string(StreamNameSep)) + 1 |
| 443 } | 441 } |
| OLD | NEW |