Skip to content

Commit

Permalink
Merge branch 'fix-backslash-in-texts' into 'master'
Browse files Browse the repository at this point in the history
[FIX][SDK] fixed backslashes being incorrectly parsed for Texts

See merge request codingame/game-engine!318
  • Loading branch information
CGjupoulton committed Sep 7, 2023
2 parents 78e3cc0 + 084c0bc commit 3a3ca30
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ static String formatFrameTime(double t) {
}

static String escape(String text) {
String escaped = text.replaceAll("\\'", "\\\\'");
if (escaped.contains(" ") || escaped.contains(";") || escaped.contains("\n")) {
if (text.contains(" ") || text.contains(";") || text.contains("\n") || text.contains("'")) {
String escaped = text.replaceAll("\\'", "\\\\'");
return "'" + escaped + "'";
}
return escaped;
return text;
}

private String serializeEntitiesStateDiff(Entity<?> entity, EntityState diff, String frameInstant) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,37 @@ function splitOnCharOutsideQuotes (text, charParam) {
const res = []
let current = ''
let idx = 0
let lastChar = ''
let isEscaped = false
let inQuotes = false


while (idx < text.length) {
const char = text[idx++]
if (char === charParam) {
if (!inQuotes) {
res.push(current)
current = ''
} else {
if (isEscaped) {
current += '\\'
isEscaped = false
}
current += char
}
} else if (char === "'" && lastChar !== '\\') {
} else if (char === "'" && !isEscaped) {
inQuotes = !inQuotes
current += char
} else if (lastChar === '\\') {
if (char === "'") {
current += "\\'"
} else {
} else if (isEscaped) {
current += '\\' + char
}
} else if (char !== '\\') {
isEscaped = false
} else if (!isEscaped && char === '\\') {
isEscaped = true
} else {
current += char
}
lastChar = char
}
if (isEscaped) {
current += '\\'
}
res.push(current)
return res
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,10 @@ export const PROPERTIES = {
}

function unescape (text) {
// replace \' by '
const unescaped = text.split("\\'").join("'")

if (unescaped.includes(' ') || unescaped.includes(';') || unescaped.includes('\n')) {
return unescaped.slice(1, unescaped.length - 1)
} else {
return unescaped
if (text.length >= 2 && text[0] === "'" && text[text.length - 1] === "'") {
const unescaped = text.slice(1, text.length - 1)
// replace \' by '
return unescaped.split("\\'").join("'")
}
return text
}
6 changes: 6 additions & 0 deletions playground/misc/misc-3-release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The CodinGame SDK is regularly updated and improved. This document lets you know what changed in the latest releases.

## 4.4.4

### 🐞 Bug fix

- Fixed `Text` not displaying or duplicating backslash characters in the viewer

## 4.4.3

### 🐞 Bug fix
Expand Down

0 comments on commit 3a3ca30

Please sign in to comment.