Skip to content

Commit 3362eca

Browse files
committed
Rename "numerize" functions to "digitize"
Rename old "digitize" functions to "dottedDecimal" Start commenting each function
1 parent 7e0612e commit 3362eca

File tree

2 files changed

+173
-81
lines changed

2 files changed

+173
-81
lines changed

IPSubnetcalc.swift

Lines changed: 138 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ class IPSubnetCalc: NSObject {
8585
//*************
8686
//IPv4 SECTION
8787
//*************
88+
/**
89+
Convert an IP address in its binary representation
90+
91+
- Parameters:
92+
- ipAddress: IP address in dotted decimal format like 192.168.1.42
93+
- space: add a space to each decimal
94+
- dotted: add a dot to each decimal
95+
96+
- Returns:
97+
the binary representation of the given IP address
98+
99+
*/
88100
static func binarize(ipAddress: String, space: Bool = false, dotted: Bool = true) -> String {
89101
var ipAddressBin = [String]()
90102
var binStr = String()
@@ -117,10 +129,30 @@ class IPSubnetCalc: NSObject {
117129
return (binStr)
118130
}
119131

132+
/**
133+
Convert the current IPv4 address to its binary representation
134+
135+
- Parameter dotted: add dot to each decimal
136+
137+
- Returns:
138+
the binary representation of the current IP address
139+
140+
*/
120141
func binaryMap(dotted: Bool = true) -> String {
121142
return (IPSubnetCalc.binarize(ipAddress: ipv4Address, space: false, dotted: dotted))
122143
}
123144

145+
/**
146+
Convert an IP address in its hexadecimal representation
147+
148+
- Parameters:
149+
- ipAddress: IP address in dotted decimal format like 192.168.1.42
150+
- dotted: add a dot to each decimal
151+
152+
- Returns:
153+
the hexadecimal representation of the given IP address
154+
155+
*/
124156
static func hexarize(ipAddress: String, dotted: Bool = true) -> String {
125157
var ipDigits = [String]()
126158
var hexIP = String()
@@ -142,11 +174,29 @@ class IPSubnetCalc: NSObject {
142174
return (hexIP)
143175
}
144176

177+
/**
178+
Convert the current IPv4 address to its hexadecimal representation
179+
180+
- Parameter dotted: add dot to each decimal
181+
182+
- Returns:
183+
the hexadecimal representation of the current IP address
184+
185+
*/
145186
func hexaMap(dotted: Bool = true) -> String {
146187
return (IPSubnetCalc.hexarize(ipAddress: ipv4Address, dotted: dotted))
147188
}
148189

149-
static func numerize(ipAddress: String) -> UInt32 {
190+
/**
191+
Convert an IP address in dotted decimal format to an UInt32 value
192+
193+
- Parameter ipAddress: an IP address in dotted decimal format like 192.168.1.42
194+
195+
- Returns:
196+
a digital IP address in UInt32 format
197+
198+
*/
199+
static func digitize(ipAddress: String) -> UInt32 {
150200
var ipAddressNum: UInt32 = 0
151201
var ipDigits = [String]()
152202

@@ -170,12 +220,29 @@ class IPSubnetCalc: NSObject {
170220
}
171221
*/
172222

173-
//numerize a Mask bits value to UInt32
174-
static func numerize(maskbits: Int) -> UInt32 {
223+
/**
224+
Convert a mask value in bits to an UInt32 value
225+
226+
- parameter maskbits: subnet mask bits as in /XX notation
227+
228+
- returns:
229+
a digital subnet mask in UInt32 format
230+
231+
*/
232+
static func digitize(maskbits: Int) -> UInt32 {
175233
return ((Constants.addr32Full << (32 - maskbits)) & Constants.addr32Full)
176234
}
177235

178-
static func digitize(ipAddress: UInt32) -> String {
236+
/**
237+
Convert an IP address in digital format to dotted decimal format
238+
239+
- parameter ipAddress: IP address in its digital format
240+
241+
- returns:
242+
a String reprensenting the dotted decimal format of the given IP address
243+
244+
*/
245+
static func dottedDecimal(ipAddress: UInt32) -> String {
179246
var ipDigits = String()
180247

181248
ipDigits.append(String(((ipAddress & Constants.addr32Digit1) >> Constants.classCbits)) + ".")
@@ -185,6 +252,18 @@ class IPSubnetCalc: NSObject {
185252
return (ipDigits)
186253
}
187254

255+
/**
256+
Check if the IP address is a valid IPv4 address
257+
258+
- Parameters:
259+
- ipAddress: IP address in dotted decimal format like 192.168.1.42
260+
- mask: Optionnal subnet mask
261+
- classless: enable class less checks of the given IP address/mask
262+
263+
- Returns:
264+
Boolean if the given IP address is valid or not
265+
266+
*/
188267
static func isValidIP(ipAddress: String, mask: String?, classless: Bool = false) -> Bool {
189268
var ip4Digits = [String]()
190269

@@ -231,36 +310,64 @@ class IPSubnetCalc: NSObject {
231310
return true
232311
}
233312

313+
/**
314+
Returns current Subnet ID address
315+
316+
- Returns:
317+
the dotted decimal representation of the Subnet ID of the current IP address/mask
318+
319+
*/
234320
func subnetId() -> String {
235321
var subnetId: UInt32 = 0
236-
let ipBits = IPSubnetCalc.numerize(ipAddress: self.ipv4Address)
237-
let maskBits = IPSubnetCalc.numerize(maskbits: self.maskBits)
322+
let ipBits = IPSubnetCalc.digitize(ipAddress: self.ipv4Address)
323+
let maskBits = IPSubnetCalc.digitize(maskbits: self.maskBits)
238324

239325
subnetId = ipBits & maskBits
240-
return (IPSubnetCalc.digitize(ipAddress: subnetId))
326+
return (IPSubnetCalc.dottedDecimal(ipAddress: subnetId))
241327
}
242328

329+
/**
330+
Returns current broadcast address
331+
332+
- Returns:
333+
the dotted decimal representation of the broadcast address of the current IP address/mask
334+
335+
*/
243336
func subnetBroadcast() -> String {
244337
var broadcast: UInt32 = 0
245-
let ipBits = IPSubnetCalc.numerize(ipAddress: self.ipv4Address)
246-
let maskBits = IPSubnetCalc.numerize(maskbits: self.maskBits)
338+
let ipBits = IPSubnetCalc.digitize(ipAddress: self.ipv4Address)
339+
let maskBits = IPSubnetCalc.digitize(maskbits: self.maskBits)
247340

248341
broadcast = ipBits & maskBits | (Constants.addr32Full >> self.maskBits)
249-
return (IPSubnetCalc.digitize(ipAddress: broadcast))
342+
return (IPSubnetCalc.dottedDecimal(ipAddress: broadcast))
250343
}
251344

345+
/**
346+
Returns current Subnet Mask
347+
348+
- Returns:
349+
String of the current subnet mask as in /XX notation
350+
351+
*/
252352
func subnetMask() -> String {
253353
var subnetMask: UInt32 = 0
254354

255355
subnetMask = Constants.addr32Full << (32 - self.maskBits)
256-
return (IPSubnetCalc.digitize(ipAddress: subnetMask))
356+
return (IPSubnetCalc.dottedDecimal(ipAddress: subnetMask))
257357
}
258358

359+
/**
360+
Returns current Wildcard Subnet Mask
361+
362+
- Returns:
363+
the dotted decimal representation of the wildcard subnet mask of the current IP address/mask
364+
365+
*/
259366
func wildcardMask() -> String {
260367
var wildcardMask: UInt32 = 0
261368

262369
wildcardMask = ~(Constants.addr32Full << (32 - self.maskBits))
263-
return (IPSubnetCalc.digitize(ipAddress: wildcardMask))
370+
return (IPSubnetCalc.dottedDecimal(ipAddress: wildcardMask))
264371
}
265372

266373
func maxHosts() -> Int {
@@ -312,14 +419,14 @@ class IPSubnetCalc: NSObject {
312419
var lastIP: UInt32 = 0
313420

314421
if (maskBits == 31 || maskBits == 32) {
315-
firstIP = IPSubnetCalc.numerize(ipAddress: subnetId())
316-
lastIP = IPSubnetCalc.numerize(ipAddress: subnetBroadcast())
422+
firstIP = IPSubnetCalc.digitize(ipAddress: subnetId())
423+
lastIP = IPSubnetCalc.digitize(ipAddress: subnetBroadcast())
317424
}
318425
else {
319-
firstIP = IPSubnetCalc.numerize(ipAddress: subnetId()) + 1
320-
lastIP = IPSubnetCalc.numerize(ipAddress: subnetBroadcast()) - 1
426+
firstIP = IPSubnetCalc.digitize(ipAddress: subnetId()) + 1
427+
lastIP = IPSubnetCalc.digitize(ipAddress: subnetBroadcast()) - 1
321428
}
322-
range = IPSubnetCalc.digitize(ipAddress: firstIP) + " - " + IPSubnetCalc.digitize(ipAddress: lastIP)
429+
range = IPSubnetCalc.dottedDecimal(ipAddress: firstIP) + " - " + IPSubnetCalc.dottedDecimal(ipAddress: lastIP)
323430
return (range)
324431
}
325432

@@ -328,14 +435,14 @@ class IPSubnetCalc: NSObject {
328435
var firstIP: UInt32 = 0
329436
var lastIP: UInt32 = 0
330437

331-
firstIP = IPSubnetCalc.numerize(ipAddress: subnetId())
332-
lastIP = IPSubnetCalc.numerize(ipAddress: subnetBroadcast())
333-
range = IPSubnetCalc.digitize(ipAddress: firstIP) + " - " + IPSubnetCalc.digitize(ipAddress: lastIP)
438+
firstIP = IPSubnetCalc.digitize(ipAddress: subnetId())
439+
lastIP = IPSubnetCalc.digitize(ipAddress: subnetBroadcast())
440+
range = IPSubnetCalc.dottedDecimal(ipAddress: firstIP) + " - " + IPSubnetCalc.dottedDecimal(ipAddress: lastIP)
334441
return (range)
335442
}
336443

337444
static func netClass(ipAddress: String) -> String {
338-
let ipNum = IPSubnetCalc.numerize(ipAddress: ipAddress)
445+
let ipNum = IPSubnetCalc.digitize(ipAddress: ipAddress)
339446
let addr1stByte = (ipNum & Constants.maskClassA) >> 24
340447

341448
if (addr1stByte < 127) {
@@ -418,7 +525,7 @@ class IPSubnetCalc: NSObject {
418525
static func maskBits(maskAddr: String) -> Int {
419526
var bits: Int = 0
420527

421-
var mask:UInt32 = IPSubnetCalc.numerize(ipAddress: maskAddr)
528+
var mask:UInt32 = IPSubnetCalc.digitize(ipAddress: maskAddr)
422529
while (mask != 0) {
423530
bits += 1
424531
mask <<= 1
@@ -536,8 +643,8 @@ class IPSubnetCalc: NSObject {
536643
print("CIDR Network (Route) : " + self.subnetId())
537644
print("CIDR Net Notation : " + self.subnetId() + "/" + String(self.maskBits))
538645
print("CIDR Address Range : " + self.subnetCIDRRange())
539-
print("IP number in binary : " + String(IPSubnetCalc.numerize(ipAddress: self.ipv4Address), radix: 2))
540-
print("Mask bin : " + String(IPSubnetCalc.numerize(maskbits: self.maskBits), radix: 2))
646+
print("IP number in binary : " + String(IPSubnetCalc.digitize(ipAddress: self.ipv4Address), radix: 2))
647+
print("Mask bin : " + String(IPSubnetCalc.digitize(maskbits: self.maskBits), radix: 2))
541648
//print("Subnet ID bin : " + String(self.subnetId(), radix: 2))
542649
//print("Broadcast bin : " + String(self.subnetBroadcast(), radix: 2))
543650
}
@@ -604,7 +711,7 @@ class IPSubnetCalc: NSObject {
604711
static func convertIPv4toIPv6(ipAddress: String, _6to4: Bool = false) -> String {
605712
var ipv6str = String()
606713

607-
let addr = numerize(ipAddress: ipAddress)
714+
let addr = digitize(ipAddress: ipAddress)
608715
ipv6str.append(String((((Constants.addr32Digit1 | Constants.addr32Digit2) & addr) >> 16), radix: 16))
609716
ipv6str.append(":")
610717
ipv6str.append(String(((Constants.addr32Digit3 | Constants.addr32Digit4) & addr), radix: 16))
@@ -663,7 +770,7 @@ class IPSubnetCalc: NSObject {
663770
}
664771
}
665772

666-
static func numerizeIPv6(ipAddress: String) -> [UInt16] {
773+
static func digitizeIPv6(ipAddress: String) -> [UInt16] {
667774
var ipAddressNum: [UInt16] = Array(repeating: 0, count: 8)
668775
var ip4Hex = [String]()
669776

@@ -698,7 +805,7 @@ class IPSubnetCalc: NSObject {
698805
return (binStr)
699806
}
700807

701-
static func numerizeMaskIPv6(maskbits: Int) -> [UInt16] {
808+
static func digitizeMaskIPv6(maskbits: Int) -> [UInt16] {
702809
var maskNum: [UInt16] = Array(repeating: 0, count: 8)
703810

704811
for i in 0...7 {
@@ -745,22 +852,7 @@ class IPSubnetCalc: NSObject {
745852
func binaryIDIPv6() -> String {
746853
return (IPSubnetCalc.binarizeIPv6(ipAddress: self.ipv6Address))
747854
}
748-
749-
func digitizeIPv6() -> String {
750-
var digitStr = String()
751-
let numIP = IPSubnetCalc.numerizeIPv6(ipAddress: self.ipv6Address)
752855

753-
for index in 0...7 {
754-
if (index != 7) {
755-
digitStr.append("\(numIP[index]):")
756-
}
757-
else {
758-
digitStr.append("\(numIP[index])")
759-
}
760-
}
761-
return (digitStr)
762-
}
763-
764856
func fullAddressIPv6(ipAddress: String) -> String {
765857
var fullAddr = String()
766858
var ip4Hex = [String]()
@@ -850,8 +942,8 @@ class IPSubnetCalc: NSObject {
850942

851943
func networkIPv6() -> String {
852944
var netID = [UInt16]()
853-
let numMask = IPSubnetCalc.numerizeMaskIPv6(maskbits: self.ipv6MaskBits)
854-
let numIP = IPSubnetCalc.numerizeIPv6(ipAddress: fullAddressIPv6(ipAddress: self.ipv6Address))
945+
let numMask = IPSubnetCalc.digitizeMaskIPv6(maskbits: self.ipv6MaskBits)
946+
let numIP = IPSubnetCalc.digitizeIPv6(ipAddress: fullAddressIPv6(ipAddress: self.ipv6Address))
855947

856948
for index in 0...7 {
857949
//print("Index: \(index) IP: \(numIP[index]) Mask : \(numMask[index]) Result : \(numIP[index] & (numMask[index])) ")
@@ -863,8 +955,8 @@ class IPSubnetCalc: NSObject {
863955
func networkRangeIPv6() -> String {
864956
var netID = [UInt16]()
865957
var netID2 = [UInt16]()
866-
let numMask = IPSubnetCalc.numerizeMaskIPv6(maskbits: self.ipv6MaskBits)
867-
let numIP = IPSubnetCalc.numerizeIPv6(ipAddress: self.ipv6Address)
958+
let numMask = IPSubnetCalc.digitizeMaskIPv6(maskbits: self.ipv6MaskBits)
959+
let numIP = IPSubnetCalc.digitizeIPv6(ipAddress: self.ipv6Address)
868960

869961
for index in 0...7 {
870962
//print("Index: \(index) IP: \(numIP[index]) Mask : \(numMask[index]) Result : \(numIP[index] & (numMask[index])) ")

0 commit comments

Comments
 (0)