Skip to content

Commit 28f95ac

Browse files
committed
Refactor Chars struct location
1 parent 410f06d commit 28f95ac

File tree

3 files changed

+102
-101
lines changed

3 files changed

+102
-101
lines changed

EntropyString.podspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
Pod::Spec.new do |s|
44
s.name = "EntropyString"
5-
s.version = "0.4.0"
6-
s.summary = "Efficiently generate secure strings of specified entropy from various character sets."
5+
s.version = "0.5.0"
6+
s.summary = "Efficiently generate secure, random strings of specified entropy from various character sets."
77

88
# This description is used to generate tags and improve search results.
99
# * Think: What does it do? Why did you write it? What is the focus?
1010
# * Try to keep it short, snappy and to the point.
1111
# * Write the description between the DESC delimiters below.
1212
# * Finally, don't worry about the indent, CocoaPods strips it!
1313
s.description = <<-DESC
14-
Efficiently generate secure strings of specified entropy from various character sets for use when probabilisticly unique string identifiers are needed. Entropy is specified by total number of strings and acceptable risk of a repeat.
14+
Efficiently generate secure, randmo strings of specified entropy from various character sets for use when probabilisticly unique string identifiers are needed. Entropy is specified by total number of strings and acceptable risk of a repeat.
1515
DESC
1616

1717
s.homepage = "https://github.com/#{s.name}/#{s.name}-Swift"

Sources/CharSet.swift

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,100 @@ public enum CharSet: UInt {
7575
}
7676
}
7777
}
78+
79+
// MARK: - Private Structs
80+
internal struct Chars {
81+
// File system and URL safe char set from RFC 4648.
82+
static let default64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
83+
84+
// Remove all upper and lower case vowels and 'Y'; all numbers that look like letters
85+
// [0,1,5], letters that look like numbers [l], and all letters that have poor distinction
86+
// between upper and lower case values.
87+
static let default32 = "2346789bdfghjmnpqrtBDFGHJLMNPQRT"
88+
89+
// Hexadecimal
90+
static let default16 = "0123456789abcdef"
91+
92+
// Octal
93+
static let default8 = "01234567"
94+
95+
// DNA alphabet
96+
static let default4 = "ATCG"
97+
98+
// Binary
99+
static let default2 = "01"
100+
101+
private var _charSet64: String?
102+
var charSet64: String {
103+
return _charSet64 ?? Chars.default64
104+
}
105+
mutating func set(charSet64: String, force: Bool) throws {
106+
guard charSet64.characters.count == Int(CharSet.charSet64.rawValue) else { throw RandomString.RandomStringError.invalidCharCount }
107+
guard force || unique(string: charSet64) else { throw RandomString.RandomStringError.charsNotUnique }
108+
_charSet64 = charSet64
109+
}
110+
111+
private var _charSet32: String?
112+
var charSet32: String {
113+
return _charSet32 ?? Chars.default32
114+
}
115+
mutating func set(charSet32: String, force: Bool) throws {
116+
guard charSet32.characters.count == Int(CharSet.charSet32.rawValue) else { throw RandomString.RandomStringError.invalidCharCount }
117+
guard force || unique(string: charSet32) else { throw RandomString.RandomStringError.charsNotUnique }
118+
_charSet32 = charSet32
119+
}
120+
121+
private var _charSet16: String?
122+
var charSet16: String {
123+
return _charSet16 ?? Chars.default16
124+
}
125+
mutating func set(charSet16: String, force: Bool) throws {
126+
guard charSet16.characters.count == Int(CharSet.charSet16.rawValue) else { throw RandomString.RandomStringError.invalidCharCount }
127+
guard force || unique(string: charSet16) else { throw RandomString.RandomStringError.charsNotUnique }
128+
_charSet16 = charSet16
129+
}
130+
131+
private var _charSet8: String?
132+
var charSet8: String {
133+
return _charSet8 ?? Chars.default8
134+
}
135+
mutating func set(charSet8: String, force: Bool) throws {
136+
guard charSet8.characters.count == Int(CharSet.charSet8.rawValue) else { throw RandomString.RandomStringError.invalidCharCount }
137+
guard force || unique(string: charSet8) else { throw RandomString.RandomStringError.charsNotUnique }
138+
_charSet8 = charSet8
139+
}
140+
141+
private var _charSet4: String?
142+
var charSet4: String {
143+
return _charSet4 ?? Chars.default4
144+
}
145+
mutating func set(charSet4: String, force: Bool) throws {
146+
guard charSet4.characters.count == Int(CharSet.charSet4.rawValue) else { throw RandomString.RandomStringError.invalidCharCount }
147+
guard force || unique(string: charSet4) else { throw RandomString.RandomStringError.charsNotUnique }
148+
_charSet4 = charSet4
149+
}
150+
151+
private var _charSet2: String?
152+
var charSet2: String {
153+
return _charSet2 ?? Chars.default2
154+
}
155+
mutating func set(charSet2: String, force: Bool) throws {
156+
guard charSet2.characters.count == Int(CharSet.charSet2.rawValue) else { throw RandomString.RandomStringError.invalidCharCount }
157+
guard force || unique(string: charSet2) else { throw RandomString.RandomStringError.charsNotUnique }
158+
_charSet2 = charSet2
159+
}
160+
161+
private func unique(string: String) -> Bool {
162+
var charSet = Set<Character>()
163+
var unique = true
164+
for char in string.characters {
165+
let (inserted, _) = charSet.insert(char)
166+
unique = unique && inserted
167+
if !unique {
168+
break
169+
}
170+
}
171+
return unique
172+
}
173+
}
78174
}

Sources/RandomString.swift

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class RandomString {
3434

3535
// MARK: - Enums
3636
/// Errors thrown by RandomString
37-
enum RandomStringError: Error {
37+
public enum RandomStringError: Error {
3838
case tooFewBytes
3939
case invalidCharCount
4040
case charsNotUnique
@@ -44,7 +44,7 @@ public class RandomString {
4444
private static let instance = RandomString()
4545

4646
// MARK: - Private Vars (Instance)
47-
private var chars: Chars
47+
private var chars: CharSet.Chars
4848

4949
// MARK: - Public API (Class)
5050
//
@@ -104,7 +104,7 @@ public class RandomString {
104104
}
105105

106106
public init() {
107-
chars = Chars()
107+
chars = CharSet.Chars()
108108
}
109109

110110
// MARK: - Public API (Instance)
@@ -428,99 +428,4 @@ public class RandomString {
428428
return a / gcd(a,b) * b
429429
}
430430

431-
// MARK: - Private Structs
432-
private struct Chars {
433-
// File system and URL safe char set from RFC 4648.
434-
static let default64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
435-
436-
// Remove all upper and lower case vowels and 'Y'; all numbers that look like letters
437-
// [0,1,5], letters that look like numbers [l], and all letters that have poor distinction
438-
// between upper and lower case values.
439-
static let default32 = "2346789bdfghjmnpqrtBDFGHJLMNPQRT"
440-
441-
// Hexadecimal
442-
static let default16 = "0123456789abcdef"
443-
444-
// Octal
445-
static let default8 = "01234567"
446-
447-
// DNA alphabet
448-
static let default4 = "ATCG"
449-
450-
// Binary
451-
static let default2 = "01"
452-
453-
private var _charSet64: String?
454-
var charSet64: String {
455-
return _charSet64 ?? Chars.default64
456-
}
457-
mutating func set(charSet64: String, force: Bool) throws {
458-
guard charSet64.characters.count == Int(CharSet.charSet64.rawValue) else { throw RandomStringError.invalidCharCount }
459-
guard force || unique(string: charSet64) else { throw RandomStringError.charsNotUnique }
460-
_charSet64 = charSet64
461-
}
462-
463-
private var _charSet32: String?
464-
var charSet32: String {
465-
return _charSet32 ?? Chars.default32
466-
}
467-
mutating func set(charSet32: String, force: Bool) throws {
468-
guard charSet32.characters.count == Int(CharSet.charSet32.rawValue) else { throw RandomStringError.invalidCharCount }
469-
guard force || unique(string: charSet32) else { throw RandomStringError.charsNotUnique }
470-
_charSet32 = charSet32
471-
}
472-
473-
private var _charSet16: String?
474-
var charSet16: String {
475-
return _charSet16 ?? Chars.default16
476-
}
477-
mutating func set(charSet16: String, force: Bool) throws {
478-
guard charSet16.characters.count == Int(CharSet.charSet16.rawValue) else { throw RandomStringError.invalidCharCount }
479-
guard force || unique(string: charSet16) else { throw RandomStringError.charsNotUnique }
480-
_charSet16 = charSet16
481-
}
482-
483-
private var _charSet8: String?
484-
var charSet8: String {
485-
return _charSet8 ?? Chars.default8
486-
}
487-
mutating func set(charSet8: String, force: Bool) throws {
488-
guard charSet8.characters.count == Int(CharSet.charSet8.rawValue) else { throw RandomStringError.invalidCharCount }
489-
guard force || unique(string: charSet8) else { throw RandomStringError.charsNotUnique }
490-
_charSet8 = charSet8
491-
}
492-
493-
private var _charSet4: String?
494-
var charSet4: String {
495-
return _charSet4 ?? Chars.default4
496-
}
497-
mutating func set(charSet4: String, force: Bool) throws {
498-
guard charSet4.characters.count == Int(CharSet.charSet4.rawValue) else { throw RandomStringError.invalidCharCount }
499-
guard force || unique(string: charSet4) else { throw RandomStringError.charsNotUnique }
500-
_charSet4 = charSet4
501-
}
502-
503-
private var _charSet2: String?
504-
var charSet2: String {
505-
return _charSet2 ?? Chars.default2
506-
}
507-
mutating func set(charSet2: String, force: Bool) throws {
508-
guard charSet2.characters.count == Int(CharSet.charSet2.rawValue) else { throw RandomStringError.invalidCharCount }
509-
guard force || unique(string: charSet2) else { throw RandomStringError.charsNotUnique }
510-
_charSet2 = charSet2
511-
}
512-
513-
private func unique(string: String) -> Bool {
514-
var charSet = Set<Character>()
515-
var unique = true
516-
for char in string.characters {
517-
let (inserted, _) = charSet.insert(char)
518-
unique = unique && inserted
519-
if !unique {
520-
break
521-
}
522-
}
523-
return unique
524-
}
525-
}
526431
}

0 commit comments

Comments
 (0)