Skip to content

Commit df88f3f

Browse files
authored
Merge pull request #2626 from DougGregor/integer-literal-utils
Improvements to integer literal utilities used in SourceKit-LSP
2 parents ae08a1c + 13f747d commit df88f3f

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Sources/SwiftRefactor/IntegerLiteralUtilities.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ extension IntegerLiteralExprSyntax {
3131
case .hex: return 16
3232
}
3333
}
34+
35+
/// The prefix that is used to express an integer literal with this
36+
/// radix in Swift source code, e.g., "0x" for hexadecimal.
37+
public var literalPrefix: String {
38+
switch self {
39+
case .binary: return "0b"
40+
case .octal: return "0o"
41+
case .hex: return "0x"
42+
case .decimal: return ""
43+
}
44+
}
3445
}
3546

3647
public var radix: Radix {
@@ -67,15 +78,8 @@ extension IntegerLiteralExprSyntax {
6778
/// ```
6879
public func split() -> (prefix: String, value: Substring) {
6980
let text = self.literal.text
70-
switch self.radix {
71-
case .binary:
72-
return ("0b", text.dropFirst(2))
73-
case .octal:
74-
return ("0o", text.dropFirst(2))
75-
case .decimal:
76-
return ("", Substring(text))
77-
case .hex:
78-
return ("0x", text.dropFirst(2))
79-
}
81+
let radix = self.radix
82+
let literalPrefix = radix.literalPrefix
83+
return (literalPrefix, text.dropFirst(literalPrefix.count))
8084
}
8185
}

0 commit comments

Comments
 (0)