Jinal-Sarvaiya commented on code in PR #150:
URL: https://github.com/apache/camel-kit/pull/150#discussion_r4482677639


##########
kit/camel-kit-core/src/main/java/org/apache/camel/kit/ship/controller/ShipStageResult.java:
##########
@@ -280,6 +282,110 @@ private static void requireReport(String report) throws 
IOException {
         utf8(report, MAX_REPORT_BYTES, "stage report");
     }
 
+    private static String extractJsonObject(String text) throws IOException {
+        if (text == null) {
+            return text;
+        }
+        String stripped = text.strip();
+        if (stripped.startsWith("{")) {
+            return stripped;
+        }
+        String fenced = extractFencedJson(text);
+        if (fenced != null) {
+            return fenced;
+        }
+        int start = text.indexOf('{');
+        int end = text.lastIndexOf('}');
+        if (start >= 0 && end > start) {
+            String prefix = text.substring(0, start).strip();
+            if (!prefix.isEmpty() && prefix.indexOf('[') >= 0) {
+                throw new IOException("Pi stage result is wrapped in an 
array");
+            }
+            return text.substring(start, end + 1);
+        }
+        return stripped;
+    }
+
+    private static String extractFencedJson(String text) throws IOException {
+        String result = null;
+        int regionStart = -1;
+        int regionEnd = -1;
+        int pos = 0;
+        int len = text.length();
+
+        while (pos < len) {
+            int tick = text.indexOf("```", pos);
+            if (tick < 0) {
+                break;
+            }
+            if (tick > 0 && text.charAt(tick - 1) != '\n') {
+                pos = tick + 3;
+                continue;
+            }
+            int cur = tick + 3;
+            if (cur + 4 <= len && text.startsWith("json", cur)) {
+                cur += 4;
+            }
+            while (cur < len && (text.charAt(cur) == ' ' || text.charAt(cur) 
== '\t')) {
+                cur++;
+            }
+            if (cur >= len || text.charAt(cur) != '\n') {
+                pos = tick + 3;
+                continue;
+            }
+            int contentStart = cur + 1;
+
+            int close = contentStart;
+            boolean found = false;
+            while (true) {
+                close = text.indexOf("\n```", close);
+                if (close < 0) {
+                    break;
+                }
+                int afterClose = close + 4;
+                if (afterClose >= len || text.charAt(afterClose) == '\n') {
+                    found = true;
+                    break;
+                }
+                close = afterClose;
+            }
+            if (!found) {
+                pos = contentStart;
+                continue;

Review Comment:
   Both quadratic paths eliminated. Regression coverage added for the exact 
inputs from the review:
   
   - "```json\n{\n".repeat(90_000) (900 KB): previously > 12 s, now completes 
in < 2 s — **committed test:** malformedFenceOpenersDoNotCauseQuadraticScan PASS
   - "\n".repeat(100_000) before one valid fenced envelope (100 KB): previously 
> 12 s, now completes in < 2 s — committed test: 
blankLinePrefixDoesNotCauseQuadraticScan PASS



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to