Skip to content

Unexpected behaviour #145

Description

Hi Salvatore,

I came across some unexpected behaviour while using the sdsMakeRoomFor() and sdsrange() functions. Please have a look below.
PS: It might be my one misuse of the library, but curious to hear what you think.

$ echo "test" >> test_file.txt

#include <stdio.h>
#include "sds/sds.h"

#define BUFSIZE 4096

static void expected_behaviour(FILE* const f) {
  sds s = sdsnew("abc");
  s     = sdsMakeRoomFor(s, BUFSIZE);

  while (fgets(s, BUFSIZE, f)) {
    sdsrange(s, 0, 2);
    printf("%s", s); // prints: tes
  }
  printf("\n");
  sdsfree(s);
}

static void unexpected_behaviour(FILE* const f) {
  // Can sdsempty() be used here?
  sds s = sdsMakeRoomFor(sdsempty(), BUFSIZE);

  while (fgets(s, BUFSIZE, f)) {
    // Why isn't sdsrange() working as expected?
    sdsrange(s, 0, 2);
    printf("%s", s); // prints: test\n
  }
  printf("\n");
  sdsfree(s);
}
int main(void) {
  FILE* const f = fopen("test_file.txt", "r");  // assume valid file
  expected_behaviour(f);
  fseek(f, 0, SEEK_SET);
  unexpected_behaviour(f);
  fclose(f)
  return 0;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions