Skip to content

Commit b8065e3

Browse files
patch logic
1 parent ba7c592 commit b8065e3

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

code/logic/cstring.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,12 @@ cstring fossil_io_cstring_pad_right(ccstring str, size_t total_length, char pad_
336336
}
337337

338338
int fossil_io_cstring_icmp(ccstring str1, ccstring str2) {
339-
if (!str1 || !str2) return (str1 == str2) ? 0 : (str1 ? 1 : -1);
339+
// Handle NULL pointers: both NULL means equal, one NULL means less/greater
340+
if (str1 == str2) return 0;
341+
if (!str1) return (str2 && *str2) ? -1 : 0;
342+
if (!str2) return (*str1) ? 1 : 0;
343+
344+
// Then check
340345
while (*str1 && *str2) {
341346
int c1 = tolower((unsigned char)*str1);
342347
int c2 = tolower((unsigned char)*str2);

code/logic/fossil/io/cstring.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,22 @@ namespace fossil {
414414
_str = fossil_io_cstring_create(init.c_str());
415415
}
416416

417+
/**
418+
* Constructor to create a CString from an existing cstring.
419+
*
420+
* @param str The cstring to wrap.
421+
*/
422+
CString(ccstring str) {
423+
_str = fossil_io_cstring_copy(str);
424+
}
425+
426+
/**
427+
* Default constructor to create an empty CString.
428+
*/
429+
CString() {
430+
_str = fossil_io_cstring_create("");
431+
}
432+
417433
/**
418434
* Destructor to free the memory allocated for the cstring.
419435
*/

code/logic/soap.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,23 @@ const char *fossil_io_soap_detect_tone(const char *text) {
387387

388388
int 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

398409
char *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
}

code/logic/stream.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ int32_t fossil_fstream_remove(const char *filename) {
330330
return FOSSIL_ERROR_OK; // File removed successfully
331331
}
332332

333+
if (errno == ENOENT) {
334+
fossil_io_fprintf(FOSSIL_STDERR, "Error: File not found when removing file %s\n", filename);
335+
return FOSSIL_ERROR_FILE_NOT_FOUND;
336+
}
337+
333338
fossil_io_fprintf(FOSSIL_STDERR, "Error: IO error when removing file %s\n", filename);
334339
return FOSSIL_ERROR_IO;
335340
}

0 commit comments

Comments
 (0)