Skip to content

Commit dd50f85

Browse files
committed
Experimental encoding support
1 parent 1d6d5bf commit dd50f85

File tree

1 file changed

+9
-59
lines changed

1 file changed

+9
-59
lines changed

Sources/_OpenAPIGeneratorCore/Extensions/String.swift

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ fileprivate extension String {
6767
}
6868

6969
// Only allow [a-zA-Z][a-zA-Z0-9_]*
70-
// This is bad, is there something like percent encoding functionality but for general "allowed chars only"?
71-
7270
let firstCharSet: CharacterSet = .letters
7371
let numbers: CharacterSet = .decimalDigits
7472
let otherCharSet: CharacterSet = .alphanumerics.union(.init(charactersIn: "_"))
@@ -83,7 +81,15 @@ fileprivate extension String {
8381
sanitizedScalars.append("_")
8482
outScalar = scalar
8583
} else {
86-
outScalar = "_"
84+
var hexString = String(scalar.value, radix: 16, uppercase: true)
85+
if let firstChar = hexString.unicodeScalars.first,
86+
!firstCharSet.contains(firstChar) {
87+
hexString = "_\(hexString)"
88+
}
89+
for char in hexString.unicodeScalars {
90+
sanitizedScalars.append(char)
91+
}
92+
continue
8793
}
8894
sanitizedScalars.append(outScalar)
8995
}
@@ -153,62 +159,6 @@ fileprivate extension String {
153159
"true",
154160
"try",
155161
"throws",
156-
"__FILE__",
157-
"__LINE__",
158-
"__COLUMN__",
159-
"__FUNCTION__",
160-
"__DSO_HANDLE__",
161-
"_",
162-
"(",
163-
")",
164-
"{",
165-
"}",
166-
"[",
167-
"]",
168-
"<",
169-
">",
170-
".",
171-
".",
172-
",",
173-
"...",
174-
":",
175-
";",
176-
"=",
177-
"@",
178-
"#",
179-
"&",
180-
"->",
181-
"`",
182-
"\\",
183-
"!",
184-
"?",
185-
"?",
186-
"\"",
187-
"\'",
188-
"\"\"\"",
189-
"#keyPath",
190-
"#line",
191-
"#selector",
192-
"#file",
193-
"#fileID",
194-
"#filePath",
195-
"#column",
196-
"#function",
197-
"#dsohandle",
198-
"#assert",
199-
"#sourceLocation",
200-
"#warning",
201-
"#error",
202-
"#if",
203-
"#else",
204-
"#elseif",
205-
"#endif",
206-
"#available",
207-
"#unavailable",
208-
"#fileLiteral",
209-
"#imageLiteral",
210-
"#colorLiteral",
211-
")",
212162
"yield",
213163
"String",
214164
"Error",

0 commit comments

Comments
 (0)