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

refactor: Use raw string resources #4109

Merged
merged 3 commits into from
Dec 12, 2024
Merged
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
@@ -1,8 +1,8 @@
package app.revanced.util.resource

import app.revanced.patcher.patch.PatchException
import org.w3c.dom.Document
import org.w3c.dom.Node
import java.util.logging.Logger

/**
* A string value.
Expand All @@ -19,13 +19,36 @@ class StringResource(
) : BaseResource(name, "string") {
override fun serialize(ownerDocument: Document, resourceCallback: (BaseResource) -> Unit) =
super.serialize(ownerDocument, resourceCallback).apply {

fun String.validateAndroidStringEscaping() : String {
if (value.startsWith('"') && value.endsWith('"')) {
// Raw strings allow unescaped single quote but not double quote.
if (!value.substring(1, value.length - 1).contains(Regex("(?<!\\\\)[\"]"))) {
return this;
}
} else {
if (value.contains('\n')) {
// Don't throw an exception, otherwise unnoticed mistakes
// in Crowdin can cause patching failures.
// Incorrectly escaped strings still work but do not display as intended.
Logger.getLogger(StringResource.javaClass.name).severe(
"String $name is not raw but contains encoded new line characters: $value")
}
if (!value.contains(Regex("(?<!\\\\)['\"]"))) {
return this;
}
}

Logger.getLogger(StringResource.javaClass.name).severe(
"String $name cannot contain unescaped quotes in value: $value")

return this;
}

// if the string is un-formatted, explicitly add the formatted attribute
if (!formatted) setAttribute("formatted", "false")

if (value.contains(Regex("(?<!\\\\)['\"]")))
throw PatchException("String $name cannot contain unescaped quotes in value \"$value\".")

textContent = value
textContent = value.validateAndroidStringEscaping();
}

companion object {
Expand Down
Loading
Loading