@@ -387,12 +387,23 @@ const char *fossil_io_soap_detect_tone(const char *text) {
387387
388388int fossil_io_soap_check_grammar (const char * text ) {
389389 if (!text ) return -1 ;
390+ int found = 0 ;
390391 for (size_t i = 0 ; FOSSIL_SOAP_GRAMMAR_SUGGESTIONS [i ].incorrect ; i ++ ) {
391- if (custom_strcasestr (text , FOSSIL_SOAP_GRAMMAR_SUGGESTIONS [i ].incorrect )) {
392- return 1 ; // Grammar issue found
392+ const char * incorrect = FOSSIL_SOAP_GRAMMAR_SUGGESTIONS [i ].incorrect ;
393+ const char * ptr = text ;
394+ while ((ptr = custom_strcasestr (ptr , incorrect )) != NULL ) {
395+ // Check word boundaries
396+ int before = (ptr == text ) || !isalnum ((unsigned char )ptr [-1 ]);
397+ int after = !isalnum ((unsigned char )ptr [strlen (incorrect )]);
398+ if (before && after ) {
399+ found = 1 ;
400+ break ;
401+ }
402+ ptr += strlen (incorrect );
393403 }
404+ if (found ) break ;
394405 }
395- return 0 ;
406+ return found ;
396407}
397408
398409char * fossil_io_soap_normalize (const char * text ) {
@@ -427,10 +438,6 @@ char *fossil_io_soap_normalize_slang(const char *text) {
427438 char * result = fossil_io_cstring_dup (text );
428439 if (!result ) return NULL ;
429440
430- for (size_t i = 0 ; result [i ]; i ++ ) {
431- result [i ] = tolower (result [i ]);
432- }
433-
434441 for (size_t i = 0 ; FOSSIL_SOAP_SUGGESTIONS [i ].bad != NULL ; i ++ ) {
435442 const char * bad = FOSSIL_SOAP_SUGGESTIONS [i ].bad ;
436443 const char * sugg = FOSSIL_SOAP_SUGGESTIONS [i ].suggested ;
@@ -528,18 +535,25 @@ char *fossil_io_soap_filter_offensive(const char *text) {
528535 while ((found = custom_strcasestr (result , bad )) != NULL ) {
529536 size_t offset = (size_t )(found - result );
530537 size_t newlen = strlen (result ) - strlen (bad ) + strlen (good ) + 1 ;
531-
532- char * temp = malloc (newlen );
538+
539+ char * temp = ( char * ) malloc (newlen );
533540 if (!temp ) {
534541 free (result );
535542 return NULL ;
536543 }
537-
538- strncpy (temp , result , offset );
544+
545+ // Copy prefix safely
546+ if (offset > 0 ) {
547+ memcpy (temp , result , offset );
548+ }
539549 temp [offset ] = '\0' ;
550+
551+ // Append replacement
540552 strcat (temp , good );
553+
554+ // Append suffix
541555 strcat (temp , result + offset + strlen (bad ));
542-
556+
543557 free (result );
544558 result = temp ;
545559 }
0 commit comments