mirror of
https://github.com/coteditor/CotEditor.git
synced 2024-09-11 11:25:57 +03:00
Fix snakecase translation
This commit is contained in:
parent
abe4b0b738
commit
ae2205ada6
@ -2,6 +2,15 @@
|
||||
Change Log
|
||||
==========================
|
||||
|
||||
3.8.8 (unreleased)
|
||||
--------------------------
|
||||
|
||||
### Fixes
|
||||
|
||||
- Fix a possible crash when transforming the case of selection that includes specific a character order.
|
||||
|
||||
|
||||
|
||||
3.8.7 (389)
|
||||
--------------------------
|
||||
|
||||
|
@ -27,18 +27,18 @@ import Foundation
|
||||
|
||||
extension String {
|
||||
|
||||
/// transform all camel and pascal case words to snake case
|
||||
/// Transform all camel and pascal case words to snake case.
|
||||
var snakecased: String {
|
||||
|
||||
return self.ranges(pattern: "(?<=\\w)[A-Z]")
|
||||
return self.ranges(pattern: "(?<=\\w)(?=[A-Z])")
|
||||
.reversed()
|
||||
.reduce(self.lowercased()) { (string, range) in
|
||||
string.replacingCharacters(in: range, with: "_" + string[range])
|
||||
string.replacingCharacters(in: range, with: "_")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// transform all snake and pascal case words to camel case
|
||||
/// Transform all snake and pascal case words to camel case.
|
||||
var camelcased: String {
|
||||
|
||||
return self.ranges(pattern: "(?<=\\w)(?:[A-Z]|_\\w)")
|
||||
@ -51,7 +51,7 @@ extension String {
|
||||
}
|
||||
|
||||
|
||||
/// transform all snake and pascal case words to pascal case
|
||||
/// Transform all snake and pascal case words to pascal case.
|
||||
var pascalcased: String {
|
||||
|
||||
return self.ranges(pattern: "(?:\\b|(?<=\\w)_)\\w")
|
||||
@ -71,7 +71,8 @@ extension String {
|
||||
|
||||
return (try! NSRegularExpression(pattern: pattern))
|
||||
.matches(in: self, range: self.nsRange)
|
||||
.map { Range($0.range, in: self)! }
|
||||
.map { $0.range }
|
||||
.compactMap { String.Index(utf16Offset: $0.lowerBound, in: self)..<String.Index(utf16Offset: $0.upperBound, in: self) }
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -126,14 +126,17 @@ final class StringExtensionsTests: XCTestCase {
|
||||
XCTAssertEqual("AbcDefg Hij".snakecased, "abc_defg hij")
|
||||
XCTAssertEqual("abcDefg Hij".snakecased, "abc_defg hij")
|
||||
XCTAssertEqual("_abcDefg Hij".snakecased, "_abc_defg hij")
|
||||
XCTAssertEqual("AA\u{0308}".snakecased, "a_a\u{0308}")
|
||||
|
||||
XCTAssertEqual("abc_defg Hij".camelcased, "abcDefg hij")
|
||||
XCTAssertEqual("AbcDefg Hij".camelcased, "abcDefg hij")
|
||||
XCTAssertEqual("_abcDefg Hij".camelcased, "_abcDefg hij")
|
||||
XCTAssertEqual("a_a\u{0308}".camelcased, "aA\u{0308}")
|
||||
|
||||
XCTAssertEqual("abc_defg Hij".pascalcased, "AbcDefg Hij")
|
||||
XCTAssertEqual("abcDefg Hij".pascalcased, "AbcDefg Hij")
|
||||
XCTAssertEqual("_abcDefg Hij".pascalcased, "_abcDefg Hij")
|
||||
XCTAssertEqual("a_a\u{0308}".pascalcased, "AA\u{0308}")
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user