Skip to content

Commit

Permalink
Use more strings.Cut + strings.Contains
Browse files Browse the repository at this point in the history
  • Loading branch information
hknutzen committed Feb 6, 2023
1 parent 8076e89 commit cc9296f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 37 deletions.
4 changes: 2 additions & 2 deletions go/pkg/api/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ func (s *state) createHost(j *job) error {
ip1 := ip
// Use attribute "range" when IP1-IP2 range is given.
// Use IP1 when searching corresponding network.
if i := strings.Index(ip, "-"); i != -1 {
if left, _, found := strings.Cut(ip, "-"); found {
attr = "range"
ip1 = ip[:i]
ip1 = left
}

// Search network matching given ip and mask.
Expand Down
7 changes: 3 additions & 4 deletions go/pkg/ast/order.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Sort list elements of AST before printing.
//
package ast

import (
Expand Down Expand Up @@ -67,11 +66,11 @@ func sortElem(l []Element) {
}

func getType(v string) string {
i := strings.Index(v, ":")
if i == -1 {
typ, _, found := strings.Cut(v, ":")
if !found {
return ""
}
return v[:i]
return typ
}

func getName(v string) string {
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/astset/astset.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,6 @@ func (s *State) removeFromToplevelAttr(typ, attr, name string) {
}

func getTypeName(v string) (string, string) {
i := strings.Index(v, ":")
return v[:i], v[i+1:]
typ, name, _ := strings.Cut(v, ":")
return typ, name
}
14 changes: 7 additions & 7 deletions go/pkg/pass1/cut-netspoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Prints a brief help message and exits.
=head1 COPYRIGHT AND DISCLAIMER
(c) 2022 by Heinz Knutzen <heinz.knutzen@googlemail.com>
(c) 2023 by Heinz Knutzen <heinz.knutzen@googlemail.com>
http://hknutzen.github.com/Netspoc
Expand Down Expand Up @@ -324,9 +324,9 @@ func (c *spoc) markAndSubstElements(
if x.isAggregate && name[0] == '[' {
name = name[1:]
ip := ""
if i := strings.Index(name, " & "); i >= 0 {
ip = name[len("ip="):i]
name = name[i+3:]
if left, right, found := strings.Cut(name, " & "); found {
ip = left[len("ip="):]
name = right
}
name = name[:len(name)-1]
a := new(ast.AggAuto)
Expand All @@ -348,9 +348,9 @@ func (c *spoc) markAndSubstElements(
a := new(ast.IntfRef)
a.Type = typ
a.Router = r
if i := strings.Index(net, "."); i >= 0 {
a.Extension = net[i+1:]
net = net[:i]
if left, right, found := strings.Cut(net, "."); found {
net = left
a.Extension = right
}
a.Network = net
result = a
Expand Down
2 changes: 1 addition & 1 deletion go/pkg/pass1/print-code.go
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ func printCaAndTunnelGroupMap(fh *os.File, id, tgName string) {
// Activate tunnel-group with tunnel-group-map.
// Use id as ca-map name.
subjectName := "ea"
if strings.Index(id, "@") == -1 {
if !strings.Contains(id, "@") {
subjectName = "cn"
}
fmt.Fprintln(fh, "crypto ca certificate map", id, "10")
Expand Down
34 changes: 19 additions & 15 deletions go/pkg/pass1/setup-objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,12 +693,14 @@ func (c *spoc) setupNetwork(v *ast.Network) {
n := c.symTable.network[netName]
n.name = name
n.ipV6 = v.IPV6
i := strings.Index(netName, "/")
if i != -1 {
n.ipType = bridgedIP
}
if i != -1 && !isSimpleName(netName[:i]) || !isSimpleName(netName[i+1:]) {
c.err("Invalid identifier in definition of '%s'", name)
{
left, right, found := strings.Cut(netName, "/")
if found {
n.ipType = bridgedIP
}
if !isSimpleName(left) || found && !isSimpleName(right) {
c.err("Invalid identifier in definition of '%s'", name)
}
}
var ldapAppend string
ipGiven := false
Expand Down Expand Up @@ -1092,15 +1094,17 @@ func (c *spoc) setupRouter(v *ast.Router) {
v6 := v.IPV6
r := c.getRouter(rName, v6)
c.allRouters = append(c.allRouters, r)
i := strings.Index(rName, "@")
if i != -1 {
r.deviceName = rName[:i]
r.vrf = rName[i+1:]
} else {
r.deviceName = rName
}
if i != -1 && !isSimpleName(rName[:i]) || !isSimpleName(rName[i+1:]) {
c.err("Invalid identifier in definition of '%s'", name)
{
left, right, found := strings.Cut(rName, "@")
if found {
r.deviceName = left
r.vrf = right
} else {
r.deviceName = rName
}
if !isSimpleName(left) || found && !isSimpleName(right) {
c.err("Invalid identifier in definition of '%s'", name)
}
}
noProtectSelf := false
var routingDefault *mcastProto
Expand Down
5 changes: 3 additions & 2 deletions go/pkg/pass2/check-acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package pass2

import (
"fmt"
"golang.org/x/exp/slices"
"io"
"net"
"os"
"strconv"
"strings"

"golang.org/x/exp/slices"

"github.com/hknutzen/Netspoc/go/pkg/fileop"
"github.com/hknutzen/Netspoc/go/pkg/oslink"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -216,7 +217,7 @@ func addPackets(a *aclInfo, l []*packet) {
}
seen[*p] = true
ipObj := func(s string) *ipNet {
if i := strings.Index(s, ":"); i != -1 {
if strings.Contains(s, ":") {
s += "/128"
} else {
s += "/32"
Expand Down
9 changes: 5 additions & 4 deletions go/pkg/pass2/spoc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package pass2
/*
Pass 2 of Netspoc - A Network Security Policy Compiler
(C) 2022 by Heinz Knutzen <heinz.knutzen@googlemail.com>
(C) 2023 by Heinz Knutzen <heinz.knutzen@googlemail.com>
http://hknutzen.github.com/Netspoc
Expand All @@ -26,13 +26,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
import (
"encoding/json"
"fmt"
"github.com/hknutzen/Netspoc/go/pkg/fileop"
"github.com/hknutzen/Netspoc/go/pkg/jcode"
"net/netip"
"os"
"os/exec"
"sort"
"strings"

"github.com/hknutzen/Netspoc/go/pkg/fileop"
"github.com/hknutzen/Netspoc/go/pkg/jcode"
)

func panicf(format string, args ...interface{}) {
Expand Down Expand Up @@ -303,7 +304,7 @@ func readJSON(path string) *routerData {
panic(err)
}
rData := new(routerData)
if i := strings.Index(path, "/ipv6/"); i != -1 {
if strings.Contains(path, "/ipv6/") {
rData.ipv6 = true
}
rData.model = jData.Model
Expand Down

0 comments on commit cc9296f

Please sign in to comment.