forked from JetBrains/kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strings.kt
97 lines (87 loc) · 3.76 KB
/
Strings.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package templates
import templates.Family.*
import templates.SequenceClass.*
object StringJoinOps : TemplateGroupBase() {
val f_joinTo = fn("joinTo(buffer: A, separator: CharSequence = \", \", prefix: CharSequence = \"\", postfix: CharSequence = \"\", limit: Int = -1, truncated: CharSequence = \"...\", transform: ((T) -> CharSequence)? = null)") {
includeDefault()
} builder {
doc {
"""
Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
elements will be appended, followed by the [truncated] string (which defaults to "...").
@sample samples.collections.Collections.Transformations.joinTo
"""
}
sequenceClassification(terminal)
typeParam("A : Appendable")
returns("A")
body {
"""
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
buffer.appendElement(element, transform)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
return buffer
"""
}
body(ArraysOfPrimitives) {
"""
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
if (transform != null)
buffer.append(transform(element))
else
buffer.append(${if (primitive == PrimitiveType.Char) "element" else "element.toString()"})
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
return buffer
"""
}
}
val f_joinToString = fn("joinToString(separator: CharSequence = \", \", prefix: CharSequence = \"\", postfix: CharSequence = \"\", limit: Int = -1, truncated: CharSequence = \"...\", transform: ((T) -> CharSequence)? = null)") {
includeDefault()
} builder {
doc {
"""
Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
elements will be appended, followed by the [truncated] string (which defaults to "...").
@sample samples.collections.Collections.Transformations.joinToString
"""
}
sequenceClassification(terminal)
returns("String")
body {
"""
return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString()
"""
}
}
}