string encode(vector<string>& strs) {
string out;
for (size_t i = 0; i < strs.size(); i++) out += (i ? ":" : "") + strs[i];
return out;
}
vector<string> decode(string s) {
vector<string> out; string cur;
for (char c : s) { if (c == ':') { out.push_back(cur); cur.clear(); } else cur += c; }
out.push_back(cur);
return out;
}
public String encode(List<String> strs) {
return String.join(":", strs);
}
public List<String> decode(String s) {
return new ArrayList<>(Arrays.asList(s.split(":", -1)));
}
def encode(strs: list[str]) -> str:
return ':'.join(strs)
def decode(s: str) -> list[str]:
return s.split(':')
function encode(strs: string[]): string {
return strs.join(':');
}
function decode(s: string): string[] {
return s.split(':');
}
string encode(vector<string>& strs) {
string out;
for (const string& s : strs) {
for (char ch : s) { if (ch == '/' || ch == ':') out += '/'; out += ch; }
out += ':';
}
return out;
}
vector<string> decode(string s) {
vector<string> out;
string cur;
for (size_t i = 0; i < s.size(); i++) {
char c = s[i];
if (c == '/') cur += s[++i];
else if (c == ':') { out.push_back(cur); cur.clear(); }
else cur += c;
}
return out;
}
public String encode(List<String> strs) {
StringBuilder sb = new StringBuilder();
for (String s : strs) sb.append(s.replace("/", "//").replace(":", "/:")).append(':');
return sb.toString();
}
public List<String> decode(String s) {
List<String> out = new ArrayList<>();
StringBuilder cur = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '/') cur.append(s.charAt(++i));
else if (c == ':') { out.add(cur.toString()); cur.setLength(0); }
else cur.append(c);
}
return out;
}
def encode(strs: list[str]) -> str:
return ''.join(s.replace('/', '//').replace(':', '/:') + ':' for s in strs)
def decode(s: str) -> list[str]:
out: list[str] = []
cur = ''
i = 0
while i < len(s):
c = s[i]
if c == '/':
i += 1
cur += s[i]
elif c == ':':
out.append(cur)
cur = ''
else: cur += c
i += 1
return out
function encode(strs: string[]): string {
return strs.map((s) => s.replaceAll('/', '//').replaceAll(':', '/:') + ':').join('');
}
function decode(s: string): string[] {
const out: string[] = [];
let cur = '';
for (let i = 0; i < s.length; i++) {
const c = s[i];
if (c === '/') cur += s[++i];
else if (c === ':') { out.push(cur); cur = ''; }
else cur += c;
}
return out;
}
string encode(vector<string>& strs) {
string out;
for (const string& s : strs) out += to_string(s.size()) + "#" + s;
return out;
}
vector<string> decode(string s) {
vector<string> out;
size_t i = 0;
while (i < s.size()) {
size_t hash = s.find('#', i);
size_t len = stoul(s.substr(i, hash - i));
out.push_back(s.substr(hash + 1, len));
i = hash + 1 + len;
}
return out;
}
public String encode(List<String> strs) {
StringBuilder out = new StringBuilder();
for (String s : strs) out.append(s.length()).append('#').append(s);
return out.toString();
}
public List<String> decode(String s) {
List<String> out = new ArrayList<>();
int i = 0;
while (i < s.length()) {
int hash = s.indexOf('#', i);
int len = Integer.parseInt(s.substring(i, hash));
out.add(s.substring(hash + 1, hash + 1 + len));
i = hash + 1 + len;
}
return out;
}
def encode(strs: list[str]) -> str:
out = ''
for s in strs: out += str(len(s)) + '#' + s
return out
def decode(s: str) -> list[str]:
out: list[str] = []
i = 0
while i < len(s):
hash_at = s.index('#', i)
length = int(s[i:hash_at])
out.append(s[hash_at + 1:hash_at + 1 + length])
i = hash_at + 1 + length
return out
function encode(strs: string[]): string {
let out = '';
for (const s of strs) out += s.length + '#' + s;
return out;
}
function decode(s: string): string[] {
const out: string[] = [];
let i = 0;
while (i < s.length) {
const hash = s.indexOf('#', i);
const len = Number(s.slice(i, hash));
out.push(s.slice(hash + 1, hash + 1 + len));
i = hash + 1 + len;
}
return out;
}
const int HEADER = 4;
const size_t MAX_LEN = 0xffff;
string encode(const vector<string>& strs) {
string out;
for (size_t i = 0; i < strs.size(); i++) {
if (strs[i].size() > MAX_LEN) throw length_error("string too long");
char hdr[HEADER + 1]; snprintf(hdr, sizeof hdr, "%04zx", strs[i].size());
out += hdr; out += strs[i];
}
return out;
}
vector<string> decode(const string& s) {
vector<string> out;
size_t i = 0;
while (i < s.size()) {
if (i + HEADER > s.size()) throw invalid_argument("truncated header");
size_t len = 0; auto r = from_chars(s.data() + i, s.data() + i + HEADER, len, 16);
if (r.ec != errc() || r.ptr != s.data() + i + HEADER) throw invalid_argument("corrupt header");
i += HEADER;
if (len > s.size() - i) throw invalid_argument("corrupt length");
out.push_back(s.substr(i, len));
i += len;
}
return out;
}
private static final int HEADER = 4;
private static final int MAX_LEN = 0xffff;
public String encode(List<String> strs) {
StringBuilder parts = new StringBuilder();
for (int i = 0; i < strs.size(); i++) {
if (strs.get(i).length() > MAX_LEN) throw new IllegalArgumentException("string too long");
parts.append(String.format("%04x", strs.get(i).length())).append(strs.get(i));
}
return parts.toString();
}
public List<String> decode(String s) {
List<String> out = new ArrayList<>();
int i = 0;
while (i < s.length()) {
if (i + HEADER > s.length()) throw new IllegalArgumentException("truncated header");
int len = Integer.parseInt(s.substring(i, i + HEADER), 16);
i += HEADER;
if (len < 0 || i + len > s.length()) throw new IllegalArgumentException("corrupt length");
out.add(s.substring(i, i + len));
i += len;
}
return out;
}
HEADER = 4
MAX_LEN = 0xFFFF
def encode(strs: list[str]) -> str:
parts: list[str] = []
for i in range(len(strs)):
if len(strs[i]) > MAX_LEN: raise ValueError('string too long')
parts.append(format(len(strs[i]), '04x') + strs[i])
return ''.join(parts)
def decode(s: str) -> list[str]:
out: list[str] = []
i = 0
while i < len(s):
if i + HEADER > len(s): raise ValueError('truncated header')
hdr = s[i:i + HEADER]
if any(c not in '0123456789abcdef' for c in hdr): raise ValueError('corrupt header')
length = int(hdr, 16)
i += HEADER
if i + length > len(s): raise ValueError('corrupt length')
out.append(s[i:i + length])
i += length
return out
const HEADER = 4;
const MAX_LEN = 0xffff;
function encode(strs: string[]): string {
const parts: string[] = new Array(strs.length);
for (let i = 0; i < strs.length; i++) {
if (strs[i].length > MAX_LEN) throw new RangeError('string too long');
parts[i] = strs[i].length.toString(16).padStart(HEADER, '0') + strs[i];
}
return parts.join('');
}
function decode(s: string): string[] {
const out: string[] = [];
let i = 0;
while (i < s.length) {
if (i + HEADER > s.length) throw new Error('truncated header');
const len = Number('0x' + s.slice(i, i + HEADER));
i += HEADER;
if (Number.isNaN(len) || i + len > s.length) throw new Error('corrupt length');
out.push(s.slice(i, i + len));
i += len;
}
return out;
}