/**
* Encodes log file output for backward compatibility.
*
* @param {string} logData The raw log file data.
* @returns {string} The encoded log data.
*/
function encodeLogData(logData) {
if (!logData) {
return ""; // Handle empty input
}
let encodedData = "";
let inString = false;
for (let i = 0; i < logData.length; i++) {
const char = logData[i];
if (char === '"' && !inString) {
// Start of a string, escape quotes
encodedData += '"';
inString = true;
} else if (char === '"' && inString) {
// End of a string, unescape quotes
encodedData += '"';
inString = false;
} else if (char === '\\') {
// Escape character, need to escape the next character
encodedData += '\\';
if (i + 1 < logData.length) {
encodedData += logData[i + 1];
i++; // Skip the escaped character
}
} else {
encodedData += char;
}
}
return encodedData;
}
// Example usage (for testing)
// const logData = `This is a log message with "quotes" and \\escaped characters.`;
// const encodedLog = encodeLogData(logData);
// console.log(encodedLog);
Add your comment