From 87bcfde75c681efcbceebfcd0df4b5422a7bedc8 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sun, 20 Oct 2019 12:49:31 +0200 Subject: [PATCH] regex simplification --- src/network_configuration_parser.cr | 62 ++++++++++------------------- 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/src/network_configuration_parser.cr b/src/network_configuration_parser.cr index 47f6b9c..5eb550e 100644 --- a/src/network_configuration_parser.cr +++ b/src/network_configuration_parser.cr @@ -32,8 +32,8 @@ class NetworkConfigurationParser up = true when /^description/ description = /^description (.+)/.match(line).try &.[1] - when /^inet6? alias .*/ - ipstr = /^inet6? alias ([a-f0-9:.\/]+)/.match(line).try &.[1] + when /^inet6? alias (?[a-f0-9:.\/]+)/ + ipstr = $~["ip"] if ipstr.nil? puts "wrong IP address alias, line #{line}" next @@ -50,8 +50,8 @@ class NetworkConfigurationParser # IP address is autoconfigured main_ip_v6 = Autoconfiguration.new - when /^inet6? .*/ - ipstr = /^inet6? ([a-f0-9:.\/]+)/.match(line).try &.[1] + when /^inet6? (?[a-f0-9:.\/]+)/ + ipstr = $~["ip"] if ipstr.nil? puts "wrong IP address, line #{line}" next @@ -62,10 +62,10 @@ class NetworkConfigurationParser main_ip_v6 = IPAddress.parse ipstr end - when /^join [^ \t]+ wpakey .*/ + when /^join (?[^ \t]+) wpakey (?[^ \t]+)/ # WPA2-PSK, other security mechanisms are not supported, yet - ssid = /^join ([^ \t]+)/.match(line).try &.[1] - wpakeystr = /^join [^ \t]+ wpakey ([^ \t]+)/.match(line).try &.[1] + ssid = $~["ssid"] + wpakeystr = $~["wpakey"] if ssid.nil? puts "wrong SSID in line: #{line}" @@ -80,31 +80,20 @@ class NetworkConfigurationParser new_ap = WirelessAPSetup.new ssid, WirelessAPSetup::WPA.new(wpakeystr) wireless_networks[ssid] = new_ap - when /^network [^ \t]+ inet6 autoconf/ - ssid = /^network ([^ \t]+)/.match(line).try &.[1] - ipstr = /^network [^ \t]+ inet6? ([^ \t]+)/.match(line).try &.[1] + when /^network (?[^ \t]+) inet6 autoconf/ + ssid = $~["ssid"] if ssid.nil? puts "wrong SSID in line: #{line}" next end - if ipstr.nil? - puts "wrong ip address in line: #{line}" - next - end - access_point = wireless_networks[ssid].not_nil! access_point.main_ip_v6 = Autoconfiguration.new - when /^network [^ \t]+ inet6? .*/ - ssid = nil - ipstr = nil - - /^network (?[^ \t]+) inet6? (?[^ \t]+)/.match(line).try do |m| - ssid = m["ssid"] - ipstr = m["ip"] - end + when /^network (?[^ \t]+) inet6? (?[^ \t]+)/ + ssid = $~["ssid"] + ipstr = $~["ip"] if ssid.nil? puts "wrong SSID in line: #{line}" @@ -127,8 +116,8 @@ class NetworkConfigurationParser puts "wrong ip address in line: #{line} (neither ipv4 or ipv6)" end - when /^network [^ ]+ dhcp6?/ - ssid = /^network (?[^ \t]+)/.match(line).try &.["ssid"] + when /^network (?[^ \t]+) dhcp6?/ + ssid = $~["ssid"] if ssid.nil? puts "wrong SSID in line: #{line}" @@ -145,14 +134,9 @@ class NetworkConfigurationParser puts "wrong dhcp instruction in line: #{line}" end - when /^network [^ ]+ dns .*/ - ssid = nil - ipstr = nil - - /^network (?[^ \t]+) dns (?[^ \t]+)/.match(line).try do |m| - ssid = m["ssid"] - ipstr = m["ip"] - end + when /^network (?[^ \t]+) dns (?[^ \t]+)/ + ssid = $~["ssid"] + ipstr = $~["ip"] if ssid.nil? puts "wrong SSID in line: #{line}" @@ -168,15 +152,11 @@ class NetworkConfigurationParser ipaddr = IPAddress.parse ipstr access_point.dns.addresses << ipaddr.to_s - when /^mtu [0-9]+/ - mtu = /^mtu ([0-9]+)/.match(line).try &.[1].to_i + when /^mtu (?[0-9]+)/ + mtu = $~["mtu"].to_i - when /^dns [^ \t]+/ - ipstr = nil - - /^dns (?[^ \t]+)/.match(line).try do |m| - ipstr = m["ip"] - end + when /^dns (?[^ \t]+)/ + ipstr = $~["ip"] if ipstr.nil? puts "wrong ip address in line: #{line}"