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 support for character codepoints #6334

Merged
merged 9 commits into from
May 8, 2024
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")
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
@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::*}",
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
})
@Since("INSERT VERSION")
public class ExprCharacterFromCodepoint extends SimplePropertyExpression<Integer, String> {

static {
Skript.registerExpression(ExprCharacterFromCodepoint.class, String.class, ExpressionType.PROPERTY,
"character [from|at] code([ ]point|position) %integer%");
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
@Nullable
public String convert(Integer integer) {
return String.valueOf((char) integer.intValue());
}
Comment on lines +55 to +57
Copy link
Member

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.


@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 src/main/java/ch/njol/skript/expressions/ExprCodepoint.java
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"
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
})
@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.length() != 1)
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
return null;
return string.codePointAt(0);
}

@Override
public Class<? extends Integer> getReturnType() {
return Integer.class;
}

@Override
protected String getPropertyName() {
return "codepoint";
}

}