Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sample for text version of find #5368

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libraries/stdlib/common/src/generated/_Strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public inline fun CharSequence.elementAtOrNull(index: Int): Char? {

/**
* Returns the first character matching the given [predicate], or `null` if no such character was found.
*
* @sample samples.collections.Collections.Elements.find
*
* @sample samples.text.Strings.find
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RohitGupta200 please make sure that these generated files are updated via :tools:kotlin-stdlib-gen:run command.
See https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/ReadMe.md#code-generation for details.

*/
@kotlin.internal.InlineOnly
public inline fun CharSequence.find(predicate: (Char) -> Boolean): Char? {
Expand Down
12 changes: 12 additions & 0 deletions libraries/stdlib/samples/test/samples/text/strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,18 @@ class Strings {
assertPrints(matchDetails(inputString, toFind, 10), "Searching for 'ever' in 'Never ever give up' starting at position 10: Not found")
}

@Sample
fun find() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sample is not compilable, please run it locally before submitting the updated version.

val text = "k1o2t3l4i5n6"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a slightly more readable string, like "kotlin 2.1.0".
While it won't affect correctness, it would be much easier to understand which characters are digits and which are letters (distinguishing l from 1 might be tricky depending on a type font).


val firstNumberInText = text.find { it.isDigit() }
Copy link
Contributor

@fzhinkin fzhinkin Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please inline an expression that uses find into assertPrints call (for example, like in the sample for last). A temporary variable doesn't add an additional context here and only distracts from grasping the semantics.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it adds some context, but it might be better to add an explicit comment like // the index of the first digit, '1' and // the string does not contain uppercase letters.

val firstUpperCaseInText = text.find { it.isUpperCase() }


assertPrints(firstNumberInText, "1")
assertPrints(firstUpperCaseInText, null)
}

@Sample
fun last() {
val string = "Kotlin 1.4.0"
Expand Down
7 changes: 6 additions & 1 deletion libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,12 @@ object Elements : TemplateGroupBase() {
} builder {
inline(Inline.Only)
doc { "Returns the first ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." }
sample("samples.collections.Collections.Elements.find")
specialFor(CharSequences) {
sample("samples.text.Strings.find")
}
specialFor(ArraysOfUnsigned) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original sample call should be left as is, without any additional specialFor predicates, and the call generating a sample specialized for char sequences should be placed right after it (wrapped into a predicate):

sample("samples.collections.Collections.Elements.find")
specialFor(CharSequences) {
    sample("samples.text.Strings.find")
}

sample("samples.collections.Collections.Elements.find")
}
returns("T?")
body { "return firstOrNull(predicate)"}
}
Expand Down