diff playground/main.c @ 126:e7899c93da77

Remove playground.
author June Park <parkjune1995@gmail.com>
date Thu, 08 Jan 2026 18:03:34 -0800
parents 1c446ab6f945
children 893d87124d16
line wrap: on
line diff
--- a/playground/main.c	Thu Jan 08 08:46:49 2026 -0800
+++ b/playground/main.c	Thu Jan 08 18:03:34 2026 -0800
@@ -1,42 +1,65 @@
+#include <stdlib.h>
 #include <stdio.h>
-#include <stdlib.h>
-
-#define REPO_ROOT "/Users/mrjunejune/zenbu"
+#include <string.h>
 
-int main()
-{
-  char command[512];
-  snprintf(command, sizeof(command), "hg -R %s serve --stdio", REPO_ROOT);
-  printf("command: %s\n", command);
-  
-  FILE *hg_pipe = popen(command, "r+");
-  if (!hg_pipe)
-  {
-    printf("Failed to open pipe\n");
-    return -1;
-  }
+int main() {
+    char *input[5] = {"Hello ", "Foo <<E", "N", "DD>", "Park <END> "};
+    char *key = "<END>";
+    int key_len = strlen(key);
 
-  fprintf(hg_pipe, "capabilities\n");
-  fflush(hg_pipe);
+    char **buffers = malloc(sizeof(char *) * 100);
+    int buffer_index = 0;
+    int key_ptr = 0;
+    for (int i = 0; i < 5; i++) {
+        char *packet = input[i];
+        int p_len = strlen(packet);
 
-  char *output = malloc(sizeof(char) * 2048);
-  char *curr = output;
-  int c;
-  int number_of_breakline = 0;
-  while ((c = fgetc(hg_pipe))  != NULL)
-  {
-    *curr++ = c;
-    printf("output: %s\n", output);
-    if (c == '\n')
-      number_of_breakline++;
-    if (number_of_breakline == 2)
-      break;
-    printf("char: %c\n", c);
-  }
-  pclose(hg_pipe);
+        for (int j = 0; j < p_len; j++) {
+            if (packet[j] == key[key_ptr]) {
+                key_ptr++;
+                
+                // If the WHOLE keyword is found
+                if (key_ptr == key_len) {
+                    // 1. Print all previous "safe" buffers
+                    // (The ones before the one where the keyword started)
+                    for (int b = 0; b < buffer_index; b++)
+                        printf("%s", buffers[b]);
+                    
+                    // 2. Handle the "Current" packet truncation
+                    // Calculate where the match started in THIS packet
+                    // If key_ptr was satisfied across multiple packets, 
+                    // 'j' is the end of the match in the current packet.
+                    int match_end_in_packet = j + 1;
+                    int match_len_in_this_packet = (key_ptr <= match_end_in_packet) ? key_ptr : match_end_in_packet;
+                    
+                    printf("%.*s\n", (match_end_in_packet - match_len_in_this_packet), packet);
+                    
+                    free(buffers);
+                    return 0;
+                }
+            } else {
+                // If a match fails, we must "flush" the buffers we were holding
+                if (key_ptr > 0) {
+                    for (int b = 0; b < buffer_index; b++) printf("%s", buffers[b]);
+                    buffer_index = 0;
+                    
+                    if (packet[j] == key[0]) key_ptr = 1;
+                    else {
+                        printf("%c", packet[j]);
+                        key_ptr = 0;
+                    }
+                } else {
+                    printf("%c", packet[j]);
+                }
+            }
+        }
 
+        // If we finish a packet and we are in the middle of a match, buffer it
+        if (key_ptr > 0) {
+            buffers[buffer_index++] = packet;
+        }
+    }
 
-  return 0;
+    free(buffers);
+    return 0;
 }
-
-