Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LatinIME
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
keyboard
LatinIME
Commits
2e4352c5
Commit
2e4352c5
authored
11 years ago
by
Jean Chalard
Committed by
Android Git Automerger
11 years ago
Browse files
Options
Downloads
Plain Diff
am
b5d5190d
: Merge "Add some utility functions."
* commit '
b5d5190d
': Add some utility functions.
parents
05314bf0
b5d5190d
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
java/src/com/android/inputmethod/latin/StringUtils.java
+71
-0
71 additions, 0 deletions
java/src/com/android/inputmethod/latin/StringUtils.java
tests/src/com/android/inputmethod/latin/StringUtilsTests.java
+79
-0
79 additions, 0 deletions
...s/src/com/android/inputmethod/latin/StringUtilsTests.java
with
150 additions
and
0 deletions
java/src/com/android/inputmethod/latin/StringUtils.java
+
71
−
0
View file @
2e4352c5
...
...
@@ -222,4 +222,75 @@ public final class StringUtils {
if
(
1
==
capsCount
)
return
CAPITALIZE_FIRST
;
return
(
letterCount
==
capsCount
?
CAPITALIZE_ALL
:
CAPITALIZE_NONE
);
}
public
static
boolean
isIdenticalAfterUpcase
(
final
String
text
)
{
final
int
len
=
text
.
length
();
for
(
int
i
=
0
;
i
<
len
;
i
=
text
.
offsetByCodePoints
(
i
,
1
))
{
final
int
codePoint
=
text
.
codePointAt
(
i
);
if
(
Character
.
isLetter
(
codePoint
)
&&
!
Character
.
isUpperCase
(
codePoint
))
{
return
false
;
}
}
return
true
;
}
public
static
boolean
isIdenticalAfterDowncase
(
final
String
text
)
{
final
int
len
=
text
.
length
();
for
(
int
i
=
0
;
i
<
len
;
i
=
text
.
offsetByCodePoints
(
i
,
1
))
{
final
int
codePoint
=
text
.
codePointAt
(
i
);
if
(
Character
.
isLetter
(
codePoint
)
&&
!
Character
.
isLowerCase
(
codePoint
))
{
return
false
;
}
}
return
true
;
}
public
static
boolean
isIdenticalAfterCapitalizeEachWord
(
final
String
text
,
final
String
separators
)
{
boolean
needCapsNext
=
true
;
final
int
len
=
text
.
length
();
for
(
int
i
=
0
;
i
<
len
;
i
=
text
.
offsetByCodePoints
(
i
,
1
))
{
final
int
codePoint
=
text
.
codePointAt
(
i
);
if
(
Character
.
isLetter
(
codePoint
))
{
if
((
needCapsNext
&&
!
Character
.
isUpperCase
(
codePoint
))
||
(!
needCapsNext
&&
!
Character
.
isLowerCase
(
codePoint
)))
{
return
false
;
}
}
// We need a capital letter next if this is a separator.
needCapsNext
=
(-
1
!=
separators
.
indexOf
(
codePoint
));
}
return
true
;
}
// TODO: like capitalizeFirst*, this does not work perfectly for Dutch because of the IJ digraph
// which should be capitalized together in *some* cases.
public
static
String
capitalizeEachWord
(
final
String
text
,
final
String
separators
,
final
Locale
locale
)
{
final
StringBuilder
builder
=
new
StringBuilder
();
boolean
needCapsNext
=
true
;
final
int
len
=
text
.
length
();
for
(
int
i
=
0
;
i
<
len
;
i
=
text
.
offsetByCodePoints
(
i
,
1
))
{
final
String
nextChar
=
text
.
substring
(
i
,
text
.
offsetByCodePoints
(
i
,
1
));
if
(
needCapsNext
)
{
builder
.
append
(
nextChar
.
toUpperCase
(
locale
));
}
else
{
builder
.
append
(
nextChar
.
toLowerCase
(
locale
));
}
// We need a capital letter next if this is a separator.
needCapsNext
=
(-
1
!=
separators
.
indexOf
(
nextChar
.
codePointAt
(
0
)));
}
return
builder
.
toString
();
}
public
static
boolean
containsAny
(
final
String
string
,
final
String
separators
)
{
final
int
len
=
separators
.
length
();
for
(
int
i
=
0
;
i
<
len
;
i
=
separators
.
offsetByCodePoints
(
i
,
1
))
{
final
int
separator
=
separators
.
codePointAt
(
i
);
if
(-
1
!=
string
.
indexOf
(
separator
))
{
return
true
;
}
}
return
false
;
}
}
This diff is collapsed.
Click to expand it.
tests/src/com/android/inputmethod/latin/StringUtilsTests.java
+
79
−
0
View file @
2e4352c5
...
...
@@ -154,4 +154,83 @@ public class StringUtilsTests extends AndroidTestCase {
assertEquals
(
StringUtils
.
CAPITALIZE_NONE
,
StringUtils
.
getCapitalizationType
(
""
));
}
public
void
testIsIdenticalAfterUpcaseIsIdenticalAfterDowncase
()
{
assertFalse
(
StringUtils
.
isIdenticalAfterUpcase
(
"capitalize"
));
assertTrue
(
StringUtils
.
isIdenticalAfterDowncase
(
"capitalize"
));
assertFalse
(
StringUtils
.
isIdenticalAfterUpcase
(
"cApITalize"
));
assertFalse
(
StringUtils
.
isIdenticalAfterDowncase
(
"cApITalize"
));
assertFalse
(
StringUtils
.
isIdenticalAfterUpcase
(
"capitalizE"
));
assertFalse
(
StringUtils
.
isIdenticalAfterDowncase
(
"capitalizE"
));
assertFalse
(
StringUtils
.
isIdenticalAfterUpcase
(
"__c a piu$@tali56ze"
));
assertTrue
(
StringUtils
.
isIdenticalAfterDowncase
(
"__c a piu$@tali56ze"
));
assertFalse
(
StringUtils
.
isIdenticalAfterUpcase
(
"A__c a piu$@tali56ze"
));
assertFalse
(
StringUtils
.
isIdenticalAfterDowncase
(
"A__c a piu$@tali56ze"
));
assertFalse
(
StringUtils
.
isIdenticalAfterUpcase
(
"Capitalize"
));
assertFalse
(
StringUtils
.
isIdenticalAfterDowncase
(
"Capitalize"
));
assertFalse
(
StringUtils
.
isIdenticalAfterUpcase
(
" Capitalize"
));
assertFalse
(
StringUtils
.
isIdenticalAfterDowncase
(
" Capitalize"
));
assertTrue
(
StringUtils
.
isIdenticalAfterUpcase
(
"CAPITALIZE"
));
assertFalse
(
StringUtils
.
isIdenticalAfterDowncase
(
"CAPITALIZE"
));
assertTrue
(
StringUtils
.
isIdenticalAfterUpcase
(
" PI26LIE"
));
assertFalse
(
StringUtils
.
isIdenticalAfterDowncase
(
" PI26LIE"
));
assertTrue
(
StringUtils
.
isIdenticalAfterUpcase
(
""
));
assertTrue
(
StringUtils
.
isIdenticalAfterDowncase
(
""
));
}
private
void
checkCapitalize
(
final
String
src
,
final
String
dst
,
final
String
separators
,
final
Locale
locale
)
{
assertEquals
(
dst
,
StringUtils
.
capitalizeEachWord
(
src
,
separators
,
locale
));
assert
(
src
.
equals
(
dst
)
==
StringUtils
.
isIdenticalAfterCapitalizeEachWord
(
src
,
separators
));
}
public
void
testCapitalizeEachWord
()
{
checkCapitalize
(
""
,
""
,
" "
,
Locale
.
ENGLISH
);
checkCapitalize
(
"test"
,
"Test"
,
" "
,
Locale
.
ENGLISH
);
checkCapitalize
(
" test"
,
" Test"
,
" "
,
Locale
.
ENGLISH
);
checkCapitalize
(
"Test"
,
"Test"
,
" "
,
Locale
.
ENGLISH
);
checkCapitalize
(
" Test"
,
" Test"
,
" "
,
Locale
.
ENGLISH
);
checkCapitalize
(
".Test"
,
".test"
,
" "
,
Locale
.
ENGLISH
);
checkCapitalize
(
".Test"
,
".Test"
,
" ."
,
Locale
.
ENGLISH
);
checkCapitalize
(
".Test"
,
".Test"
,
". "
,
Locale
.
ENGLISH
);
checkCapitalize
(
"test and retest"
,
"Test And Retest"
,
" ."
,
Locale
.
ENGLISH
);
checkCapitalize
(
"Test and retest"
,
"Test And Retest"
,
" ."
,
Locale
.
ENGLISH
);
checkCapitalize
(
"Test And Retest"
,
"Test And Retest"
,
" ."
,
Locale
.
ENGLISH
);
checkCapitalize
(
"Test And.Retest "
,
"Test And.Retest "
,
" ."
,
Locale
.
ENGLISH
);
checkCapitalize
(
"Test And.retest "
,
"Test And.Retest "
,
" ."
,
Locale
.
ENGLISH
);
checkCapitalize
(
"Test And.retest "
,
"Test And.retest "
,
" "
,
Locale
.
ENGLISH
);
checkCapitalize
(
"Test And.Retest "
,
"Test And.retest "
,
" "
,
Locale
.
ENGLISH
);
checkCapitalize
(
"test and ietest"
,
"Test And İetest"
,
" ."
,
new
Locale
(
"tr"
));
checkCapitalize
(
"test and ietest"
,
"Test And Ietest"
,
" ."
,
Locale
.
ENGLISH
);
checkCapitalize
(
"Test&Retest"
,
"Test&Retest"
,
" \n.!?*()&"
,
Locale
.
ENGLISH
);
checkCapitalize
(
"Test&retest"
,
"Test&Retest"
,
" \n.!?*()&"
,
Locale
.
ENGLISH
);
checkCapitalize
(
"test&Retest"
,
"Test&Retest"
,
" \n.!?*()&"
,
Locale
.
ENGLISH
);
checkCapitalize
(
"rest\nrecreation! And in the end..."
,
"Rest\nRecreation! And In The End..."
,
" \n.!?*,();&"
,
Locale
.
ENGLISH
);
checkCapitalize
(
"lorem ipsum dolor sit amet"
,
"Lorem Ipsum Dolor Sit Amet"
,
" \n.,!?*()&;"
,
Locale
.
ENGLISH
);
checkCapitalize
(
"Lorem!Ipsum (Dolor) Sit * Amet"
,
"Lorem!Ipsum (Dolor) Sit * Amet"
,
" \n,.;!?*()&"
,
Locale
.
ENGLISH
);
checkCapitalize
(
"Lorem!Ipsum (dolor) Sit * Amet"
,
"Lorem!Ipsum (Dolor) Sit * Amet"
,
" \n,.;!?*()&"
,
Locale
.
ENGLISH
);
}
public
void
testContainsAny
()
{
assertFalse
(
StringUtils
.
containsAny
(
""
,
" "
));
assertFalse
(
StringUtils
.
containsAny
(
"test and retest"
,
""
));
assertTrue
(
StringUtils
.
containsAny
(
"test and retest"
,
"x3iq o"
));
assertTrue
(
StringUtils
.
containsAny
(
"test and retest"
,
"x3iqo "
));
assertTrue
(
StringUtils
.
containsAny
(
"test and retest"
,
" x3iqo"
));
assertFalse
(
StringUtils
.
containsAny
(
"test and retest"
,
"x3iqo"
));
assertTrue
(
StringUtils
.
containsAny
(
"test and retest"
,
"tse "
));
assertTrue
(
StringUtils
.
containsAny
(
"test and retest."
,
".?()"
));
assertFalse
(
StringUtils
.
containsAny
(
"test and retest"
,
".?()"
));
// Surrogate pair
assertTrue
(
StringUtils
.
containsAny
(
"test and \uD861\uDED7 retest."
,
"\uD861\uDED7"
));
// Ill-formed string
assertFalse
(
StringUtils
.
containsAny
(
"test and \uD861 retest."
,
"\uD861\uDED7"
));
// Ill-formed string
assertFalse
(
StringUtils
.
containsAny
(
"test and \uDED7 retest."
,
"\uD861\uDED7"
));
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment