Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/common.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
#include "common.h"
#include "common.h"

std::string escapeJsonString(const std::string& input) {
std::string output;
output.reserve(input.size());

for(size_t i = 0; i < input.size(); i++) {
const unsigned char c = static_cast<unsigned char>(input[i]);
switch(c) {
case '"':
output += "\\\"";
break;
case '\\':
output += "\\\\";
break;
case '\b':
output += "\\b";
break;
case '\f':
output += "\\f";
break;
case '\n':
output += "\\n";
break;
case '\r':
output += "\\r";
break;
case '\t':
output += "\\t";
break;
default:
if(c < 0x20) {
const char* hex = "0123456789abcdef";
output += "\\u00";
output += hex[(c >> 4) & 0x0F];
output += hex[c & 0x0F];
} else {
output += static_cast<char>(c);
}
break;
}
}

return output;

Copilot AI Apr 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

escapeJsonString is formatted with tab indentation, while most of the codebase uses spaces (e.g., other .cpp files in src/). Please reformat this function to avoid mixed whitespace and keep style consistent.

Suggested change
std::string output;
output.reserve(input.size());
for(size_t i = 0; i < input.size(); i++) {
const unsigned char c = static_cast<unsigned char>(input[i]);
switch(c) {
case '"':
output += "\\\"";
break;
case '\\':
output += "\\\\";
break;
case '\b':
output += "\\b";
break;
case '\f':
output += "\\f";
break;
case '\n':
output += "\\n";
break;
case '\r':
output += "\\r";
break;
case '\t':
output += "\\t";
break;
default:
if(c < 0x20) {
const char* hex = "0123456789abcdef";
output += "\\u00";
output += hex[(c >> 4) & 0x0F];
output += hex[c & 0x0F];
} else {
output += static_cast<char>(c);
}
break;
}
}
return output;
std::string output;
output.reserve(input.size());
for(size_t i = 0; i < input.size(); i++) {
const unsigned char c = static_cast<unsigned char>(input[i]);
switch(c) {
case '"':
output += "\\\"";
break;
case '\\':
output += "\\\\";
break;
case '\b':
output += "\\b";
break;
case '\f':
output += "\\f";
break;
case '\n':
output += "\\n";
break;
case '\r':
output += "\\r";
break;
case '\t':
output += "\\t";
break;
default:
if(c < 0x20) {
const char* hex = "0123456789abcdef";
output += "\\u00";
output += hex[(c >> 4) & 0x0F];
output += hex[c & 0x0F];
} else {
output += static_cast<char>(c);
}
break;
}
}
return output;

Copilot uses AI. Check for mistakes.
}
3 changes: 3 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef COMMON_H
#define COMMON_H

#include <string>

#define FUSIONSCAN_VER "0.8.0"

#define _DEBUG true
Expand Down Expand Up @@ -42,5 +44,6 @@ static const int PACK_IN_MEM_LIMIT = 100;
static const int DUPE_NORMAL_LEVEL = -1;
static const int DUPE_HIGH_LEVEL = -2;

std::string escapeJsonString(const std::string& input);

#endif /* COMMON_H */
26 changes: 13 additions & 13 deletions src/jsonreporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ extern string command;

void JsonReporter::run() {
mFile << "{" << endl;
mFile << "\t\"command\":\"" << command << "\"," << endl;
mFile << "\t\"command\":\"" << escapeJsonString(command) << "\"," << endl;
mFile << "\t\"version\":\"" << FUSIONSCAN_VER << "\"," << endl;
mFile << "\t\"time\":\"" << getCurrentSystemTime() << "\"," << endl;
mFile << "\t\"time\":\"" << escapeJsonString(getCurrentSystemTime()) << "\"," << endl;

Copilot AI Apr 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

escapeJsonString(...) allocates a new std::string for every call. Since JsonReporter::run() calls it inside nested loops (per fusion and then per read via printReadToJson), this can add overhead for large JSON reports. Consider adding a helper that writes escaped content directly to the stream (or reuses a buffer) to avoid repeated allocations/copies.

Copilot uses AI. Check for mistakes.
mFile << "\t\"fusions\":{";

bool isFirstMut = true;
Expand All @@ -42,25 +42,25 @@ void JsonReporter::run() {
else
mFile << "," << endl;

mFile << "\t\t\"" << fusion.mTitle << "\":{" << endl;
mFile << "\t\t\"" << escapeJsonString(fusion.mTitle) << "\":{" << endl;
mFile << "\t\t\t\"" << "left" << "\":{" << endl;
mFile << "\t\t\t\t\"" << "gene_name" << "\":" << "\"" << fusion.mLeftGene.mName << "\"," << endl;
mFile << "\t\t\t\t\"" << "gene_chr" << "\":" << "\"" << fusion.mLeftGene.mChr << "\"," << endl;
mFile << "\t\t\t\t\"" << "gene_name" << "\":" << "\"" << escapeJsonString(fusion.mLeftGene.mName) << "\"," << endl;
mFile << "\t\t\t\t\"" << "gene_chr" << "\":" << "\"" << escapeJsonString(fusion.mLeftGene.mChr) << "\"," << endl;
mFile << "\t\t\t\t\"" << "position" << "\":" << fusion.mLeftGene.genePos2ChrPos(fusion.mLeftGP.position) << "," << endl;
mFile << "\t\t\t\t\"" << "reference" << "\":" << "\"" << fusion.mLeftRef << "\"," << endl;
mFile << "\t\t\t\t\"" << "ref_ext" << "\":" << "\"" << fusion.mLeftRefExt << "\"," << endl;
mFile << "\t\t\t\t\"" << "pos_str" << "\":" << "\"" << fusion.mLeftPos << "\"," << endl;
mFile << "\t\t\t\t\"" << "reference" << "\":" << "\"" << escapeJsonString(fusion.mLeftRef) << "\"," << endl;
mFile << "\t\t\t\t\"" << "ref_ext" << "\":" << "\"" << escapeJsonString(fusion.mLeftRefExt) << "\"," << endl;
mFile << "\t\t\t\t\"" << "pos_str" << "\":" << "\"" << escapeJsonString(fusion.mLeftPos) << "\"," << endl;
mFile << "\t\t\t\t\"" << "exon_or_intron" << "\":" << "\"" << (fusion.mLeftIsExon?"exon":"intron") << "\"," << endl;
mFile << "\t\t\t\t\"" << "exon_or_intron_id" << "\":" << fusion.mLeftExonOrIntronID << "," << endl;
mFile << "\t\t\t\t\"" << "strand" << "\":" << "\"" << (fusion.isLeftProteinForward()?"forward":"reversed") << "\"" << endl;
mFile << "\t\t\t}, " << endl;
mFile << "\t\t\t\"" << "right" << "\":{" << endl;
mFile << "\t\t\t\t\"" << "gene_name" << "\":" << "\"" << fusion.mRightGene.mName << "\"," << endl;
mFile << "\t\t\t\t\"" << "gene_chr" << "\":" << "\"" << fusion.mRightGene.mChr << "\"," << endl;
mFile << "\t\t\t\t\"" << "gene_name" << "\":" << "\"" << escapeJsonString(fusion.mRightGene.mName) << "\"," << endl;
mFile << "\t\t\t\t\"" << "gene_chr" << "\":" << "\"" << escapeJsonString(fusion.mRightGene.mChr) << "\"," << endl;
mFile << "\t\t\t\t\"" << "position" << "\":" << fusion.mRightGene.genePos2ChrPos(fusion.mRightGP.position) << "," << endl;
mFile << "\t\t\t\t\"" << "reference" << "\":" << "\"" << fusion.mRightRef << "\"," << endl;
mFile << "\t\t\t\t\"" << "ref_ext" << "\":" << "\"" << fusion.mRightRefExt << "\"," << endl;
mFile << "\t\t\t\t\"" << "pos_str" << "\":" << "\"" << fusion.mRightPos << "\"," << endl;
mFile << "\t\t\t\t\"" << "reference" << "\":" << "\"" << escapeJsonString(fusion.mRightRef) << "\"," << endl;
mFile << "\t\t\t\t\"" << "ref_ext" << "\":" << "\"" << escapeJsonString(fusion.mRightRefExt) << "\"," << endl;
mFile << "\t\t\t\t\"" << "pos_str" << "\":" << "\"" << escapeJsonString(fusion.mRightPos) << "\"," << endl;
mFile << "\t\t\t\t\"" << "exon_or_intron" << "\":" << "\"" << (fusion.mRightIsExon?"exon":"intron") << "\"," << endl;
mFile << "\t\t\t\t\"" << "exon_or_intron_id" << "\":" << fusion.mRightExonOrIntronID << "," << endl;
mFile << "\t\t\t\t\"" << "strand" << "\":" << "\"" << (fusion.isRightProteinForward()?"forward":"reversed") << "\"" << endl;
Expand Down
4 changes: 2 additions & 2 deletions src/match.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ int Match::countUnique(vector<Match*>& matches) {
}

void Match::printReadToJson(ofstream& file, string pad) {
file << pad << "\"seq\":" << "\"" << mRead->mSeq.mStr << "\"," << endl;
file << pad << "\"qual\":" << "\"" << mRead->mQuality << "\"" << endl;
file << pad << "\"seq\":" << "\"" << escapeJsonString(mRead->mSeq.mStr) << "\"," << endl;
file << pad << "\"qual\":" << "\"" << escapeJsonString(mRead->mQuality) << "\"" << endl;
}
Loading