Does the input break out?

Pasted between single quotes, input is safe only if it cannot end the string. In SQL, a quote inside a string is written as two quotes ('').

What to build

For each input, say whether it would break out of a quoted SQL string, and print the correctly escaped literal.

Requirements

  • Input breaks out if it contains a single quote that is not part of a pair of quotes. Scan from the left: two quotes in a row are one escaped quote; a quote on its own breaks out.
  • Print breaks out or stays inside, then a space, then the safe literal: the input with every single quote doubled, wrapped in single quotes.

Input

One input per line.

Output

One line per input.

Constraints

1 to 30 lines of up to 100 characters.

Example

Input:

rahim
' OR '1'='1' --

Output:

stays inside 'rahim'
breaks out ''' OR ''1''=''1'' --'

Why it works

Inside a SQL string the only way out is an unpaired quote, so a scan that pairs quotes finds every break-out; doubling every quote makes all of them pairs, which is what escaping does, though parameters remain the better fix.

Examples

Example 1 · Input
rahim
' OR '1'='1' --
Expected output
stays inside 'rahim'
breaks out ''' OR ''1''=''1'' --'
Example 2 · Input
o''brien
Expected output
stays inside 'o''''brien'

Hints

Hint 1

Walk with an index: if you see '' skip both characters; if you see a single ' alone, it breaks out.

Hint 2

text.replace(/'/g, "''") doubles every quote.

Solution

Show one correct solution (try it yourself first)
let line = prompt();
while (line !== null) {
  let breaks = false;
  for (let k = 0; k < line.length; k++) {
    if (line[k] === "'") {
      if (line[k + 1] === "'") k++;
      else {
        breaks = true;
        break;
      }
    }
  }
  console.log(`${breaks ? "breaks out" : "stays inside"} '${line.replace(/'/g, "''")}'`);
  line = prompt();
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: SQL injection and parameterized queries →