1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.ui.text
18 
19 import androidx.compose.ui.text.intl.Locale
20 import androidx.compose.ui.text.intl.LocaleList
21 import androidx.compose.ui.text.intl.PlatformLocale
22 import androidx.compose.ui.text.platform.ActualStringDelegate
23 
24 /** Interface for providing platform dependent string related operations. */
25 internal interface PlatformStringDelegate {
26     /**
27      * Implementation must return uppercase transformed String.
28      *
29      * @param string an input string
30      * @param locale a locale object
31      * @return a transformed string
32      */
toUpperCasenull33     fun toUpperCase(string: String, locale: PlatformLocale): String
34 
35     /**
36      * Implementation must return lowercase transformed String.
37      *
38      * @param string an input string
39      * @param locale a locale object
40      * @return a transformed string
41      */
42     fun toLowerCase(string: String, locale: PlatformLocale): String
43 
44     /**
45      * Implementation must return capitalized String.
46      *
47      * @param string an input string
48      * @param locale a locale object
49      * @return a transformed string
50      */
51     fun capitalize(string: String, locale: PlatformLocale): String
52 
53     /**
54      * Implementation must return decapitalized String.
55      *
56      * @param string an input string
57      * @param locale a locale object
58      * @return a transformed string
59      */
60     fun decapitalize(string: String, locale: PlatformLocale): String
61 }
62 
63 /**
64  * Returns uppercase transformed String.
65  *
66  * @param locale a locale object
67  * @return a transformed text
68  */
69 fun String.toUpperCase(locale: Locale): String =
70     stringDelegate.toUpperCase(this, locale.platformLocale)
71 
72 /**
73  * Returns lowercase transformed String.
74  *
75  * @param locale a locale object
76  * @return a transformed text
77  */
78 fun String.toLowerCase(locale: Locale): String =
79     stringDelegate.toLowerCase(this, locale.platformLocale)
80 
81 /**
82  * Returns capitalized String.
83  *
84  * @param locale a locale object
85  * @return a transformed text
86  */
87 fun String.capitalize(locale: Locale): String =
88     stringDelegate.capitalize(this, locale.platformLocale)
89 
90 /**
91  * Returns decapitalized String.
92  *
93  * @param locale a locale object
94  * @return a transformed text
95  */
96 fun String.decapitalize(locale: Locale): String =
97     stringDelegate.decapitalize(this, locale.platformLocale)
98 
99 /**
100  * Returns uppercase transformed String.
101  *
102  * @param localeList a locale list object. If empty locale list object is passed, use current locale
103  *   instead.
104  * @return a transformed text
105  */
106 fun String.toUpperCase(localeList: LocaleList): String =
107     if (localeList.isEmpty()) toUpperCase(Locale.current) else toUpperCase(localeList[0])
108 
109 /**
110  * Returns lowercase transformed String.
111  *
112  * @param localeList a locale list object. If empty locale list object is passed, use current locale
113  *   instead.
114  * @return a transformed text
115  */
116 fun String.toLowerCase(localeList: LocaleList): String =
117     if (localeList.isEmpty()) toLowerCase(Locale.current) else toLowerCase(localeList[0])
118 
119 /**
120  * Returns capitalized String.
121  *
122  * @param localeList a locale list object. If empty locale list object is passed, use current locale
123  *   instead.
124  * @return a transformed text
125  */
126 fun String.capitalize(localeList: LocaleList): String =
127     if (localeList.isEmpty()) capitalize(Locale.current) else capitalize(localeList[0])
128 
129 /**
130  * Returns decapitalized String.
131  *
132  * @param localeList a locale list object. If empty locale list object is passed, use current locale
133  *   instead.
134  */
135 fun String.decapitalize(localeList: LocaleList): String =
136     if (localeList.isEmpty()) decapitalize(Locale.current) else decapitalize(localeList[0])
137 
138 private val stringDelegate = ActualStringDelegate()
139