Skip to content

Commit 4703696

Browse files
committed
Refactor
charSet -> charset charSetNN -> charsetNN
1 parent 200c231 commit 4703696

File tree

5 files changed

+173
-173
lines changed

5 files changed

+173
-173
lines changed

Sources/Bytes.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ internal struct Bytes {
4545
/// The number of bytes returned is sufficient to generate a string with `bits` of entropy using `CharSet`
4646
///
4747
/// - parameter bits: Entropy bits
48-
/// - paramater charSet: The character set that will be used.
48+
/// - paramater charset: The character set that will be used.
4949
/// - parameter secRand: On Apple OSes, if _secRand_ is `true`, attempt to use `SecRandomCopyBytes` to
5050
/// generate random bytes; if `false` use `arc4random_buf`. This parameter is ignored on Linux OS.
5151
///
5252
/// - return: Random bytes. On Apple OSes, if _secRand_ is passed as `true`, the value on return
5353
/// indicates whether `SecRandomCopyBytes` (`true`) or `arc4random_buf` (`false`) was used.
5454
///
55-
static func random(_ bits: Float, _ charSet: CharSet, _ secRand: inout Bool) -> [UInt8] {
56-
let bytesNeeded = charSet.bytesNeeded(bits: bits)
55+
static func random(_ bits: Float, _ charset: CharSet, _ secRand: inout Bool) -> [UInt8] {
56+
let bytesNeeded = charset.bytesNeeded(bits: bits)
5757
var bytes = [UInt8](repeating: 0, count: bytesNeeded)
5858

5959
#if os(Linux)

Sources/CharSet.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,26 @@ public struct CharSet {
3232

3333
// Predefined `CharSet`s
3434
/// RFC 4648 URL and file system safe characters
35-
public static let charSet64 = try! CharSet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
35+
public static let charset64 = try! CharSet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
3636

3737
/// From a..z,A..Z,0..9
3838
/// Remove all upper and lower case vowels (including y)
3939
/// Remove all numbers that look like letters
4040
/// Remove all letters that look like numbers
4141
/// Remove all letters that have poor distinction between upper and lower case values
42-
public static let charSet32 = try! CharSet("2346789bdfghjmnpqrtBDFGHJLMNPQRT")
42+
public static let charset32 = try! CharSet("2346789bdfghjmnpqrtBDFGHJLMNPQRT")
4343

4444
/// Hexadecimal
45-
public static let charSet16 = try! CharSet("0123456789abcdef")
45+
public static let charset16 = try! CharSet("0123456789abcdef")
4646

4747
/// Octal
48-
public static let charSet8 = try! CharSet("01234567")
48+
public static let charset8 = try! CharSet("01234567")
4949

5050
/// DNA alphabet
51-
public static let charSet4 = try! CharSet("ATCG")
51+
public static let charset4 = try! CharSet("ATCG")
5252

5353
/// Binary
54-
public static let charSet2 = try! CharSet("01")
54+
public static let charset2 = try! CharSet("01")
5555

5656
/// String of characters in this `CharSet`
5757
public private(set) var chars: String
@@ -159,10 +159,10 @@ public struct CharSet {
159159
///
160160
/// - return: `true` if no repeat characters in `CharSet`
161161
private static func unique(_ string: String) -> Bool {
162-
var charSet = Set<Character>()
162+
var charset = Set<Character>()
163163
var unique = true
164164
for char in string {
165-
let (inserted, _) = charSet.insert(char)
165+
let (inserted, _) = charset.insert(char)
166166
unique = unique && inserted
167167
if !unique {
168168
break

Sources/Entropy.swift

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ public enum EntropyStringError: Error {
3737
public class Entropy {
3838
static let bitsPerByte: UInt8 = 8
3939

40-
public private(set) var charSet: CharSet
40+
public private(set) var charset: CharSet
4141

4242
// MARK: - Public Initializers
4343
//
4444
/// Create a `Entropy` instance
4545
///
46-
/// - parameter charSet: The default `CharSet`
47-
public init(_ charSet: CharSet) {
48-
self.charSet = charSet
46+
/// - parameter charset: The default `CharSet`
47+
public init(_ charset: CharSet) {
48+
self.charset = charset
4949
}
5050

51-
/// Create a `Entropy` instance with default `CharSet` set to `.charSet32`
51+
/// Create a `Entropy` instance with default `CharSet` set to `.charset32`
5252
///
5353
convenience public init() {
54-
self.init(CharSet.charSet32)
54+
self.init(CharSet.charset32)
5555
}
5656

5757
/// Create a `Entropy` instance
@@ -61,8 +61,8 @@ public class Entropy {
6161
/// - throws: `.invalidCharCount` if String length not a multiple of 2
6262
/// - throws: `.charsNotUnique` if any character repeats
6363
convenience public init(_ chars: String) throws {
64-
let charSet = try CharSet(chars)
65-
self.init(charSet)
64+
let charset = try CharSet(chars)
65+
self.init(charset)
6666
}
6767

6868
// MARK: - Public Static
@@ -82,9 +82,9 @@ public class Entropy {
8282
//
8383
/// Sets the default `CharSet` for generating random strings
8484
///
85-
/// - paramter charSet: The `CharSet` to use
86-
public func use(_ charSet: CharSet) {
87-
self.charSet = charSet
85+
/// - paramter charset: The `CharSet` to use
86+
public func use(_ charset: CharSet) {
87+
self.charset = charset
8888
}
8989

9090
/// Sets the default `CharSet` to use
@@ -94,14 +94,14 @@ public class Entropy {
9494
/// - throws: `.invalidCharCount` if String length not a multiple of 2
9595
/// - throws: `.charsNotUnique` if any character repeats
9696
public func use(_ chars: String) throws {
97-
let charSet = try CharSet(chars)
98-
self.charSet = charSet
97+
let charset = try CharSet(chars)
98+
self.charset = charset
9999
}
100100

101101
/// The characters of the default `CharSet`
102-
@available(*, deprecated, message: "use charSet.chars instead")
102+
@available(*, deprecated, message: "use charset.chars instead")
103103
public var chars: String {
104-
return charSet.chars
104+
return charset.chars
105105
}
106106

107107
// MARK: - Public API
@@ -115,80 +115,80 @@ public class Entropy {
115115

116116
/// Generates a small ID
117117
///
118-
/// - parameter charSet: The `CharSet` to use
118+
/// - parameter charset: The `CharSet` to use
119119
///
120120
/// - return: A string with a one in a million chance of repeat in 30 strings.
121-
public func smallID(_ charSet: CharSet) -> String {
121+
public func smallID(_ charset: CharSet) -> String {
122122
var secRand = true
123-
return string(bits: 29, charSet: charSet, secRand: &secRand)
123+
return string(bits: 29, charset: charset, secRand: &secRand)
124124
}
125125

126126
/// Generates a medium ID
127127
///
128128
/// - return: A string with a one in a billion chance of repeat in a million strings.
129129
public func mediumID() -> String {
130-
return mediumID(self.charSet)
130+
return mediumID(self.charset)
131131
}
132132

133133
/// Generates a medium ID
134134
///
135-
/// - parameter charSet: The `CharSet` to use
135+
/// - parameter charset: The `CharSet` to use
136136
///
137137
/// - return: A string with a one in a billion chance of repeat in a million strings.
138-
public func mediumID(_ charSet: CharSet) -> String {
138+
public func mediumID(_ charset: CharSet) -> String {
139139
var secRand = true
140-
return string(bits: 69, charSet: charSet, secRand: &secRand)
140+
return string(bits: 69, charset: charset, secRand: &secRand)
141141
}
142142

143143
/// Generates a large ID
144144
///
145145
/// - return: A string with a one in a trillion chance of repeat in a billion strings.
146146
public func largeID() -> String {
147-
return largeID(self.charSet)
147+
return largeID(self.charset)
148148
}
149149

150150
/// Generates a large ID
151151
///
152-
/// - parameter charSet: The `CharSet` to use
152+
/// - parameter charset: The `CharSet` to use
153153
///
154154
/// - return: A string with a one in a trillion chance of repeat in a billion strings.
155-
public func largeID(_ charSet: CharSet) -> String {
155+
public func largeID(_ charset: CharSet) -> String {
156156
var secRand = true
157-
return string(bits: 99, charSet: charSet, secRand: &secRand)
157+
return string(bits: 99, charset: charset, secRand: &secRand)
158158
}
159159

160160
/// Generates a 128 bit random session ID.
161161
///
162162
/// - return: A string suitable for a OWASP recommended session ID.
163163
public func sessionID() -> String {
164-
return sessionID(self.charSet)
164+
return sessionID(self.charset)
165165
}
166166

167167
/// Generates a 128 bit random session ID.
168168
///
169-
/// - parameter charSet: The `CharSet` to use
169+
/// - parameter charset: The `CharSet` to use
170170
///
171171
/// - return: A string suitable for a OWASP recommended session ID.
172-
public func sessionID(_ charSet: CharSet) -> String {
172+
public func sessionID(_ charset: CharSet) -> String {
173173
var secRand = true
174-
return string(bits: 128, charSet: charSet, secRand: &secRand)
174+
return string(bits: 128, charset: charset, secRand: &secRand)
175175
}
176176

177177
/// Generates a 256 bit random token
178178
///
179179
/// - return: A 256 bit string
180180
public func token() -> String {
181-
return token(self.charSet)
181+
return token(self.charset)
182182
}
183183

184184
/// Generates a 256 bit random token
185185
///
186-
/// - parameter charSet: The `CharSet` to use
186+
/// - parameter charset: The `CharSet` to use
187187
///
188188
/// - return: A 256 bit string
189-
public func token(_ charSet: CharSet) -> String {
189+
public func token(_ charset: CharSet) -> String {
190190
var secRand = true
191-
return string(bits: 256, charSet: charSet, secRand: &secRand)
191+
return string(bits: 256, charset: charset, secRand: &secRand)
192192
}
193193

194194
/// Generates a random string.
@@ -210,9 +210,9 @@ public class Entropy {
210210
/// - return: A string. The returned string's entropy is a multiple of the _entropy per char_
211211
/// for the character set in use. The entropy returned is the smallest such multiple larger
212212
/// than `bits`.
213-
public func string(bits: Float, charSet: CharSet) -> String {
213+
public func string(bits: Float, charset: CharSet) -> String {
214214
var secRand = true
215-
return string(bits: bits, charSet: charSet, secRand: &secRand)
215+
return string(bits: bits, charset: charset, secRand: &secRand)
216216
}
217217

218218
/// Generates a random string.
@@ -229,13 +229,13 @@ public class Entropy {
229229
/// If _secRand_ is passed in as `true`, the value of _secRand_ on return indicates whether
230230
/// `SecRandomCopyBytes` (`true`) or `arc4random_buf` (`false`) was used.
231231
public func string(bits: Float, secRand: inout Bool) -> String {
232-
return string(bits:bits, charSet: self.charSet, secRand: &secRand)
232+
return string(bits:bits, charset: self.charset, secRand: &secRand)
233233
}
234234

235235
/// Generates a random string.
236236
///
237237
/// - parameter bits: Minimum bits of entropy.
238-
/// - parameter charSet: The `CharSet` to use
238+
/// - parameter charset: The `CharSet` to use
239239
/// - parameter secRand: If _secRand_ is `true`, attempt to use `SecRandomCopyBytes` to
240240
/// generate the random bytes used to generate the random characters for the returned string;
241241
/// otherwise use `arc4random_buf` to generate random bytes.
@@ -246,12 +246,12 @@ public class Entropy {
246246
///
247247
/// If _secRand_ is passed in as `true`, the value of _secRand_ on return indicates whether
248248
/// `SecRandomCopyBytes` (`true`) or `arc4random_buf` (`false`) was used.
249-
public func string(bits: Float, charSet: CharSet, secRand: inout Bool) -> String {
249+
public func string(bits: Float, charset: CharSet, secRand: inout Bool) -> String {
250250
guard 0 < bits else { return "" }
251251
// `Bytes.random` sets `secRand`
252-
let bytes = Bytes.random(bits, charSet, &secRand)
252+
let bytes = Bytes.random(bits, charset, &secRand)
253253
// `Bytes.random` ensures enough bytes so this call will not fail
254-
return try! string(bits: bits, charSet: charSet, using: bytes)
254+
return try! string(bits: bits, charset: charset, using: bytes)
255255
}
256256

257257
/// Generates a random string.
@@ -265,43 +265,43 @@ public class Entropy {
265265
/// for the character set in use. The entropy returned is the smallest such multiple larger
266266
/// than `bits`.
267267
public func string(bits: Float, using bytes: [UInt8]) throws -> String {
268-
return try string(bits: bits, charSet: self.charSet, using: bytes)
268+
return try string(bits: bits, charset: self.charset, using: bytes)
269269
}
270270

271271
/// Generates a random string.
272272
///
273273
/// - parameter bits: Minimum bits of entropy.
274-
/// - parameter charSet: The `CharSet` to use
274+
/// - parameter charset: The `CharSet` to use
275275
/// - parameter bytes: `Bytes` used to generate characters.
276276
///
277277
/// - throws: `.tooFewBytes` if there are an insufficient number of bytes to generate the string.
278278
///
279279
/// - return: A string. The returned string's entropy is a multiple of the _entropy per char_
280280
/// for the character set in use. The entropy returned is the smallest such multiple larger
281281
/// than `bits`.
282-
public func string(bits: Float, charSet: CharSet, using bytes: [UInt8]) throws -> String {
282+
public func string(bits: Float, charset: CharSet, using bytes: [UInt8]) throws -> String {
283283
guard !(bits < 0) else { throw EntropyStringError.negativeEntropy }
284284
guard 0 < bytes.count else { return "" }
285285

286-
let count: Int = Int(ceil(bits / Float(charSet.bitsPerChar)))
286+
let count: Int = Int(ceil(bits / Float(charset.bitsPerChar)))
287287
guard 0 < count else { return "" }
288288

289-
let needed = Int(ceil(Float(charSet.bitsPerChar)/8 * Float(count)))
289+
let needed = Int(ceil(Float(charset.bitsPerChar)/8 * Float(count)))
290290
guard needed <= bytes.count else { throw EntropyStringError.tooFewBytes }
291291

292-
let chunks = count / Int(charSet.charsPerChunk)
293-
let partials = UInt8(count % Int(charSet.charsPerChunk))
292+
let chunks = count / Int(charset.charsPerChunk)
293+
let partials = UInt8(count % Int(charset.charsPerChunk))
294294

295295
var string = ""
296296
for chunk in 0 ..< chunks {
297-
for slice in 0 ..< charSet.charsPerChunk {
298-
let ndx = charSet.ndxFn(bytes, chunk, slice)
299-
string.append(char(ndx, charSet))
297+
for slice in 0 ..< charset.charsPerChunk {
298+
let ndx = charset.ndxFn(bytes, chunk, slice)
299+
string.append(char(ndx, charset))
300300
}
301301
}
302302
for slice in 0 ..< partials {
303-
let ndx = charSet.ndxFn(bytes, chunks, slice)
304-
string.append(char(ndx, charSet))
303+
let ndx = charset.ndxFn(bytes, chunks, slice)
304+
string.append(char(ndx, charset))
305305
}
306306
return string
307307
}
@@ -314,7 +314,7 @@ public class Entropy {
314314
///
315315
/// - return: The character
316316
private func char(_ ndx: CharSet.Ndx) -> Character {
317-
return char(ndx, self.charSet)
317+
return char(ndx, self.charset)
318318
}
319319

320320
/// Gets a character from the specified `CharSet` characters.
@@ -323,8 +323,8 @@ public class Entropy {
323323
/// - parameter chars: The characters string
324324
///
325325
/// - return: The character
326-
private func char(_ ndx: CharSet.Ndx, _ charSet: CharSet) -> Character {
327-
let chars = charSet.chars
326+
private func char(_ ndx: CharSet.Ndx, _ charset: CharSet) -> Character {
327+
let chars = charset.chars
328328
guard Int(ndx) < chars.count else { fatalError("Index out of bounds") }
329329
let charIndex = chars.index(chars.startIndex, offsetBy: Int(ndx))
330330
return chars[charIndex]

0 commit comments

Comments
 (0)