Skip to content

Commit 5bea187

Browse files
committed
[Omit needless words] Fix lowercasing of initialisms with two-letter initial words.
Fixes rdar://problem/26643039.
1 parent 011e306 commit 5bea187

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

lib/Basic/StringExtras.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -857,14 +857,13 @@ camel_case::toLowercaseInitialisms(StringRef string,
857857
// Lowercase until we hit the an uppercase letter followed by a
858858
// non-uppercase letter.
859859
llvm::SmallString<32> scratchStr;
860-
scratchStr.push_back(clang::toLowercase(string[0]));
861-
for (unsigned i = 1, n = string.size(); i != n; ++i) {
860+
for (unsigned i = 0, n = string.size(); i != n; ++i) {
862861
// If the next character is not uppercase, stop.
863862
if (i < n - 1 && !clang::isUppercase(string[i+1])) {
864863
// If the next non-uppercase character was not a letter, we seem
865-
// to have a plural, we should still lowercase the character
866-
// we're on.
867-
if (!clang::isLetter(string[i+1]) ||
864+
// to have a plural, or we're at the beginning, we should still
865+
// lowercase the character we're on.
866+
if (i == 0 || !clang::isLetter(string[i+1]) ||
868867
isPluralSuffix(camel_case::getFirstWord(string.substr(i+1)))) {
869868
scratchStr.push_back(clang::toLowercase(string[i]));
870869
++i;

test/ClangModules/MixedSource/mixed-nsuinteger.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var pp: AutoreleasingUnsafeMutablePointer<AnyObject?>? = nil
1515
var userTypedObj = NSUIntTest()
1616

1717
// Check that the NSUInteger comes across as UInt from user Obj C modules.
18-
var ur: UInt = userTypedObj.myCustomMethodThatOperates(onnsuIntegers: ui)
18+
var ur: UInt = userTypedObj.myCustomMethodThatOperates(onNSUIntegers: ui)
1919

2020
userTypedObj.intProp = ui
2121
userTypedObj.typedefProp = ui

test/IDE/Inputs/custom-modules/OmitNeedlessWords.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
typedef NS_OPTIONS(NSUInteger, OMWWobbleOptions) {
1111
OMWWobbleSideToSide = 0x01,
12-
OMWWobbleBackAndForth = 0x02
12+
OMWWobbleBackAndForth = 0x02,
13+
OMWWobbleToXMLHex = 0x04
1314
};
1415

1516
@interface OmitNeedlessWords : NSObject

test/IDE/print_omit_needless_words.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@
260260
// CHECK-APPKIT: func add(_: Rect)
261261
// CHECK-APPKIT: class func conjureRect(_: Rect)
262262

263+
// CHECK-OMIT-NEEDLESS-WORDS: struct OMWWobbleOptions
264+
// CHECK-OMIT-NEEDLESS-WORDS: static var sideToSide: OMWWobbleOptions
265+
// CHECK-OMIT-NEEDLESS-WORDS: static var backAndForth: OMWWobbleOptions
266+
// CHECK-OMIT-NEEDLESS-WORDS: static var toXMLHex: OMWWobbleOptions
267+
263268
// CHECK-OMIT-NEEDLESS-WORDS: func jump(to: URL)
264269
// CHECK-OMIT-NEEDLESS-WORDS: func objectIs(compatibleWith: AnyObject) -> Bool
265270
// CHECK-OMIT-NEEDLESS-WORDS: func insetBy(x: Int, y: Int)

unittests/Basic/StringExtrasTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ TEST(ToLowercaseTest, Words) {
192192
EXPECT_EQ(camel_case::toLowercaseWord("", scratch), "");
193193
}
194194

195+
TEST(ToLowercaseInitialismsTest, Words) {
196+
llvm::SmallString<64> scratch;
197+
198+
EXPECT_EQ(camel_case::toLowercaseInitialisms("ToXML", scratch), "toXML");
199+
EXPECT_EQ(camel_case::toLowercaseInitialisms("URLsInHand", scratch),
200+
"urlsInHand");
201+
}
202+
195203
TEST(ToSentencecaseTest, Words) {
196204
llvm::SmallString<64> scratch;
197205

0 commit comments

Comments
 (0)