-
-
Notifications
You must be signed in to change notification settings - Fork 373
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 support for character codepoints #6334
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
893ac2b
Add support for character codepoints
UnderscoreTud 8b241b0
Merge branch 'dev/feature' into feature/codepoints
sovdeeth 6d896be
Requested Changes
UnderscoreTud d700b79
Add tests
UnderscoreTud ee07af3
Merge branch 'dev/feature' into feature/codepoints
Moderocky c8146de
Update src/main/java/ch/njol/skript/expressions/ExprCodepoint.java
UnderscoreTud 002908f
fix pattern
UnderscoreTud 054763f
format example annotation
UnderscoreTud b654878
Merge branch 'dev/feature' into feature/codepoints
Moderocky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/main/java/ch/njol/skript/expressions/ExprCharacterFromCodepoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* This file is part of Skript. | ||
* | ||
* Skript is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Skript is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Skript. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Copyright Peter Güttinger, SkriptLang team and contributors | ||
*/ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Examples; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.expressions.base.SimplePropertyExpression; | ||
import ch.njol.skript.lang.ExpressionType; | ||
import org.bukkit.event.Event; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
@Name("Character from Codepoint") | ||
@Description("Returns the character at the specified codepoint") | ||
@Examples({ | ||
"function chars_between(lower: string, upper: string) :: strings:", | ||
"\tset {_lower} to codepoint of {_lower}", | ||
"\treturn {_none} if {_lower} is not set", | ||
"", | ||
"\tset {_upper} to codepoint of {_upper}", | ||
"\treturn {_none} if {_upper} is not set", | ||
"", | ||
"\tloop integers between {_lower} and {_upper}:", | ||
"\t\tadd character from codepoint loop-value to {_chars::*}", | ||
"\treturn {_chars::*}", | ||
}) | ||
@Since("INSERT VERSION") | ||
public class ExprCharacterFromCodepoint extends SimplePropertyExpression<Integer, String> { | ||
|
||
static { | ||
Skript.registerExpression(ExprCharacterFromCodepoint.class, String.class, ExpressionType.PROPERTY, | ||
"character (from|at|with) code([ ]point| position) %integer%"); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public String convert(Integer integer) { | ||
return String.valueOf((char) integer.intValue()); | ||
} | ||
|
||
@Override | ||
public Class<? extends String> getReturnType() { | ||
return String.class; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return "character at codepoint " + getExpr().toString(event, debug); | ||
} | ||
|
||
@Override | ||
protected String getPropertyName() { | ||
assert false; | ||
return null; | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/ch/njol/skript/expressions/ExprCodepoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* This file is part of Skript. | ||
* | ||
* Skript is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Skript is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Skript. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Copyright Peter Güttinger, SkriptLang team and contributors | ||
*/ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Examples; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.expressions.base.SimplePropertyExpression; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
@Name("Character Codepoint") | ||
@Description("Returns the Unicode codepoint of a character") | ||
@Examples({ | ||
"function is_in_order(letters: strings) :: boolean:", | ||
"\tloop {_letters::*}:", | ||
"\t\tset {_codepoint} to codepoint of lowercase loop-value", | ||
"", | ||
"\t\treturn false if {_codepoint} is not set # 'loop-value is not a single character'", | ||
"", | ||
"\t\tif:", | ||
"\t\t\t{_previous-codepoint} is set", | ||
"\t\t\t# if the codepoint of the current character is not", | ||
"\t\t\t# 1 more than the codepoint of the previous character", | ||
"\t\t\t# then the letters are not in order", | ||
"\t\t\t{_codepoint} - {_previous-codepoint} is not 1", | ||
"\t\tthen:", | ||
"\t\t\treturn false", | ||
"", | ||
"\t\tset {_previous-codepoint} to {_codepoint}", | ||
"\treturn true" | ||
}) | ||
@Since("INSERT VERSION") | ||
public class ExprCodepoint extends SimplePropertyExpression<String, Integer> { | ||
|
||
static { | ||
register(ExprCodepoint.class, Integer.class, "[unicode|character] code([ ]point| position)", "strings"); | ||
UnderscoreTud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
@Nullable | ||
public Integer convert(String string) { | ||
if (string.isEmpty()) | ||
return null; | ||
return string.codePointAt(0); | ||
} | ||
|
||
@Override | ||
public Class<? extends Integer> getReturnType() { | ||
return Integer.class; | ||
} | ||
|
||
@Override | ||
protected String getPropertyName() { | ||
return "codepoint"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
test "codepoint": | ||
assert character from codepoint 65 is "A" with "character from codepoint 65 is not 'A'" | ||
assert codepoint of "A" is 65 with "codepoint of 'A' is not 65" | ||
assert codepoint of "ABC" is 65 with "codepoint of 'A' is not 65" | ||
assert codepoint of "" is not set with "codepoint of an empty string is set" | ||
assert codepoint of (character from codepoint -1) is 65535 with "character from codepoint does not wrap around" | ||
assert codepoint of (character from codepoint infinity value) is 65535 with "codepoint of infinity value is not 65535" | ||
assert codepoint of (character from codepoint NaN value) is 0 with "codepoint of NaN value is not 0" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it's worth clamping values instead of allowing negative values to wrap around? I think it's fine either way but -1 = 65535 might be a bit confusing for users.