1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15+ // +gobra
16+
1517package addr
1618
1719import (
@@ -24,6 +26,8 @@ import (
2426
2527// ParseFormattedIA parses an IA that was formatted with the FormatIA function.
2628// The same options must be provided to successfully parse.
29+ // @ trusted
30+ // @ requires false
2731func ParseFormattedIA (ia string , opts ... FormatOption ) (IA , error ) {
2832 parts := strings .Split (ia , "-" )
2933 if len (parts ) != 2 {
@@ -33,15 +37,17 @@ func ParseFormattedIA(ia string, opts ...FormatOption) (IA, error) {
3337 if err != nil {
3438 return 0 , serrors .WrapStr ("parsing ISD part" , err , "value" , ia )
3539 }
36- as , err := ParseFormattedAS (parts [1 ], opts ... )
40+ as_ , err := ParseFormattedAS (parts [1 ], opts ... )
3741 if err != nil {
3842 return 0 , serrors .WrapStr ("parsing AS part" , err , "value" , ia )
3943 }
40- return MustIAFrom (isd , as ), nil
44+ return MustIAFrom (isd , as_ ), nil
4145}
4246
4347// ParseFormattedISD parses an ISD number that was formatted with the FormatISD
4448// function. The same options must be provided to successfully parse.
49+ // @ trusted
50+ // @ requires false
4551func ParseFormattedISD (isd string , opts ... FormatOption ) (ISD , error ) {
4652 o := applyFormatOptions (opts )
4753 if o .defaultPrefix {
@@ -56,29 +62,35 @@ func ParseFormattedISD(isd string, opts ...FormatOption) (ISD, error) {
5662
5763// ParseFormattedAS parses an AS number that was formatted with the FormatAS
5864// function. The same options must be provided to successfully parse.
59- func ParseFormattedAS (as string , opts ... FormatOption ) (AS , error ) {
65+ // @ trusted
66+ // @ requires false
67+ func ParseFormattedAS (as_ string , opts ... FormatOption ) (AS , error ) {
6068 o := applyFormatOptions (opts )
6169 if o .defaultPrefix {
62- trimmed := strings .TrimPrefix (as , "AS" )
63- if trimmed == as {
64- return 0 , serrors .New ("prefix is missing" , "prefix" , "AS" , "value" , as )
70+ trimmed := strings .TrimPrefix (as_ , "AS" )
71+ if trimmed == as_ {
72+ return 0 , serrors .New ("prefix is missing" , "prefix" , "AS" , "value" , as_ )
6573 }
66- as = trimmed
74+ as_ = trimmed
6775 }
68- return parseAS (as , o .separator )
76+ return parseAS (as_ , o .separator )
6977}
7078
7179// FormatIA formats the ISD-AS.
80+ // @ trusted
81+ // @ requires false
7282func FormatIA (ia IA , opts ... FormatOption ) string {
7383 o := applyFormatOptions (opts )
74- as := fmtAS (ia .AS (), o .separator )
84+ as_ := fmtAS (ia .AS (), o .separator )
7585 if o .defaultPrefix {
76- return fmt .Sprintf ("ISD%d-AS%s" , ia .ISD (), as )
86+ return fmt .Sprintf ("ISD%d-AS%s" , ia .ISD (), as_ )
7787 }
78- return fmt .Sprintf ("%d-%s" , ia .ISD (), as )
88+ return fmt .Sprintf ("%d-%s" , ia .ISD (), as_ )
7989}
8090
8191// FormatISD formats the ISD number.
92+ // @ trusted
93+ // @ requires false
8294func FormatISD (isd ISD , opts ... FormatOption ) string {
8395 o := applyFormatOptions (opts )
8496 if o .defaultPrefix {
@@ -88,24 +100,28 @@ func FormatISD(isd ISD, opts ...FormatOption) string {
88100}
89101
90102// FormatAS formats the AS number.
91- func FormatAS (as AS , opts ... FormatOption ) string {
103+ // @ trusted
104+ // @ requires false
105+ func FormatAS (as_ AS , opts ... FormatOption ) string {
92106 o := applyFormatOptions (opts )
93- s := fmtAS (as , o .separator )
107+ s := fmtAS (as_ , o .separator )
94108 if o .defaultPrefix {
95109 return "AS" + s
96110 }
97111 return s
98112}
99113
100- func fmtAS (as AS , sep string ) string {
101- if ! as .inRange () {
102- return fmt .Sprintf ("%d [Illegal AS: larger than %d]" , as , MaxAS )
114+ // @ trusted
115+ // @ requires as_.inRange()
116+ func fmtAS (as_ AS , sep string ) string {
117+ if ! as_ .inRange () {
118+ return fmt .Sprintf ("%d [Illegal AS: larger than %d]" , as_ , MaxAS )
103119 }
104- // Format BGP ASes as decimal
105- if as <= MaxBGPAS {
106- return strconv .FormatUint (uint64 (as ), 10 )
120+ // Format BGP ASes as_ decimal
121+ if as_ <= MaxBGPAS {
122+ return strconv .FormatUint (uint64 (as_ ), 10 )
107123 }
108- // Format all other ASes as 'sep'-separated hex.
124+ // Format all other ASes as_ 'sep'-separated hex.
109125 const maxLen = len ("ffff:ffff:ffff" )
110126 var b strings.Builder
111127 b .Grow (maxLen )
@@ -114,18 +130,20 @@ func fmtAS(as AS, sep string) string {
114130 b .WriteString (sep )
115131 }
116132 shift := uint (asPartBits * (asParts - i - 1 ))
117- b .WriteString (strconv .FormatUint (uint64 (as >> shift )& asPartMask , asPartBase ))
133+ b .WriteString (strconv .FormatUint (uint64 (as_ >> shift )& asPartMask , asPartBase ))
118134 }
119135 return b .String ()
120136}
121137
122- type FormatOption func (* formatOptions )
138+ type FormatOption = func (* formatOptions )
123139
124140type formatOptions struct {
125141 defaultPrefix bool
126142 separator string
127143}
128144
145+ // @ trusted
146+ // @ requires false
129147func applyFormatOptions (opts []FormatOption ) formatOptions {
130148 o := formatOptions {
131149 defaultPrefix : false ,
@@ -139,6 +157,8 @@ func applyFormatOptions(opts []FormatOption) formatOptions {
139157
140158// WithDefaultPrefix enables the default prefix which depends on the type. For
141159// the AS number, the prefix is 'AS'. For the ISD number, the prefix is 'ISD'.
160+ // @ trusted
161+ // @ requires false
142162func WithDefaultPrefix () FormatOption {
143163 return func (o * formatOptions ) {
144164 o .defaultPrefix = true
@@ -147,13 +167,17 @@ func WithDefaultPrefix() FormatOption {
147167
148168// WithSeparator sets the separator to use for formatting AS numbers. In case of
149169// the empty string, the ':' is used.
170+ // @ trusted
171+ // @ requires false
150172func WithSeparator (separator string ) FormatOption {
151173 return func (o * formatOptions ) {
152174 o .separator = separator
153175 }
154176}
155177
156178// WithFileSeparator returns an option that sets the separator to underscore.
179+ // @ trusted
180+ // @ requires false
157181func WithFileSeparator () FormatOption {
158182 return WithSeparator ("_" )
159183}
0 commit comments