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 isBase64 string property #743

Open
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ class KotlinCodeGenerator(

val containRegexProperty = properties.values.any { it.isRegex() }

fun PClass.Property.isBase64(): Boolean =
(this.type as? PType.Class)?.pClass?.info == PClassInfo.Base64

val containBase64Property = properties.values.any { it.isBase64() }

fun generateConstructor(): FunSpec {
val builder = FunSpec.constructorBuilder()
for ((name, property) in allProperties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.LoopNode;
import org.apache.commons.codec.binary.Base64;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.*;
Expand Down Expand Up @@ -148,6 +149,13 @@ protected boolean eval(String self) {
}
}

public abstract static class isBase64 extends ExternalPropertyNode {
@Specialization
protected boolean eval(String self) {
return Base64.isArrayByteBase64(self.getBytes());
}
}

public abstract static class chars extends ExternalPropertyNode {
@Specialization
@TruffleBoundary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ facts {
!str1.endsWith("efx")
}

["isBase64"] {
quickBrownFox.base64.isBase64
}

["isRegex"] {
str1.isRegex
#"\w*[0-9]"#.isRegex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ facts {
str2.base64.base64Decoded == str2
}

["isBase64"] {
str1.isBase64
}

["contains()"] {
str1.contains(str2)
str1.contains(str1)
Expand Down Expand Up @@ -304,12 +308,12 @@ examples {
str1.sha1
str2.sha1
}

["sha256"] {
str1.sha256
str2.sha256
}

["sha256Int"] {
str1.sha256Int
str2.sha256Int
Expand All @@ -319,7 +323,7 @@ examples {
str1.md5
str2.md5
}

["base64"] {
str1.base64
str2.base64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ facts {
true
true
}
["isBase64"] {
true
}
["toBoolean()"] {
true
true
Expand Down Expand Up @@ -175,8 +178,8 @@ examples {
}
["trimStart()"] {
"""
abcdefg \t
abcdefg \t

"""
}
["trimEnd()"] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ facts {
true
true
}
["isBase64"] {
true
}
["capitalize"] {
true
true
Expand Down Expand Up @@ -126,8 +129,8 @@ examples {
}
["trimStart()"] {
"""
😀😈😍😎😡🤢🤣 \t
😀😈😍😎😡🤢🤣 \t

"""
}
["trimEnd()"] {
Expand Down
9 changes: 6 additions & 3 deletions stdlib/base.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,9 @@ external class String extends Any {
/// Tells if this string is a valid regular expression according to [Regex].
external isRegex: Boolean

/// Tells if this string is a valid base64 string according to [Base64].
external isBase64: Boolean

/// The [MD5](https://en.wikipedia.org/wiki/MD5)
/// hash of this string's UTF-8 byte sequence
/// as hexadecimal string.
Expand Down Expand Up @@ -1368,7 +1371,7 @@ external class String extends Any {
external function split(pattern: String|Regex): List<String>

/// Splits this string matches of [pattern], up to [limit] substrings.
///
///
/// Returns a [List] with at most [limit] elements.
/// If the limit has been reached, the last entry will contain the un-split remainder of this string.
///
Expand All @@ -1379,7 +1382,7 @@ external class String extends Any {
/// "a.b.c".splitLimit(".", 50) == List("a", "b", "c")
/// "a.b:c".splitLimit(Regex("[.:]"), 3) == List("a", "b", "c")
/// ```
@Since { version = "0.27.0" }
@Since { version = "0.27.0" }
external function splitLimit(pattern: String|Regex, limit: Int(this > 0)): List<String>

/// Converts the first character of this string to title case.
Expand Down Expand Up @@ -3047,7 +3050,7 @@ external class Set<out Element> extends Collection<Element> {

/// The intersection of this set and [other].
external function intersect(other: Set): Set<Element>

/// The difference of this set and [other].
external function difference(other: Set): Set<Element>
}
Expand Down