-while ((c = *t++))
- if (c == '\'') count++;
-
-t = quoted = store_get_quoted(Ustrlen(s) + count + 1, s, idx, US"ibase");
-
-while ((c = *s++))
- if (c == '\'') { *t++ = '\''; *t++ = '\''; }
- else *t++ = c;
-
-*t = 0;
-return quoted;
+for (uschar c; c = *s; s++)
+ {
+ if (c == '\'') quoted = string_catn(quoted, US"\\", 1);
+ quoted = string_catn(quoted, s, 1);
+ }
+gstring_release_unused(quoted);
+return(string_from_gstring(quoted));