-
-
Notifications
You must be signed in to change notification settings - Fork 8
Store contents of static string struct in the array buffer #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
887d6e5
Handle possible NULL pointers in the array buffer
ngoldbaum ec0c8e6
refactor so contents of ss struct is in array buffer
ngoldbaum c786670
respond to review comments; clarify integer return codes
ngoldbaum 78f6771
don't use load_string with output parameter
ngoldbaum f1b2489
Fix null check in load_string
ngoldbaum ff238ae
Fix error handling in casts
ngoldbaum 01f13a9
fix reference counting error
ngoldbaum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,76 @@ | ||
#include "static_string.h" | ||
|
||
// allocates a new ss string of length len, filling with the contents of init | ||
ss * | ||
ssnewlen(const char *init, size_t len) | ||
int | ||
ssnewlen(const char *init, size_t len, ss *to_init) | ||
{ | ||
if ((to_init->buf != NULL) || (to_init->len != 0)) { | ||
return -1; | ||
} | ||
|
||
// one extra byte for null terminator | ||
ss *ret = (ss *)malloc(sizeof(ss) + sizeof(char) * (len + 1)); | ||
char *ret_buf = (char *)malloc(sizeof(char) * (len + 1)); | ||
|
||
if (ret == NULL) { | ||
return NULL; | ||
if (ret_buf == NULL) { | ||
return -1; | ||
} | ||
|
||
ret->len = len; | ||
to_init->len = len; | ||
|
||
if (len > 0) { | ||
memcpy(ret->buf, init, len); | ||
memcpy(ret_buf, init, len); | ||
} | ||
|
||
ret->buf[len] = '\0'; | ||
ret_buf[len] = '\0'; | ||
|
||
to_init->buf = ret_buf; | ||
|
||
return 0; | ||
} | ||
|
||
return ret; | ||
void | ||
ssfree(ss *str) | ||
{ | ||
if (str->buf != NULL) { | ||
free(str->buf); | ||
str->buf = NULL; | ||
} | ||
str->len = 0; | ||
} | ||
|
||
// returns a new heap-allocated copy of input string *s* | ||
ss * | ||
ssdup(const ss *s) | ||
int | ||
ssdup(ss *in, ss *out) | ||
{ | ||
return ssnewlen(s->buf, s->len); | ||
return ssnewlen(in->buf, in->len, out); | ||
} | ||
|
||
// returns a new, empty string of length len | ||
// does not do any initialization, the caller must | ||
// initialize and null-terminate the string | ||
ss * | ||
ssnewempty(size_t len) | ||
int | ||
ssnewemptylen(size_t num_bytes, ss *out) | ||
{ | ||
ss *ret = (ss *)malloc(sizeof(ss) + sizeof(char) * (len + 1)); | ||
ret->len = len; | ||
return ret; | ||
if (out->len != 0 || out->buf != NULL) { | ||
return -1; | ||
} | ||
|
||
char *buf = (char *)malloc(sizeof(char) * (num_bytes + 1)); | ||
|
||
if (buf == NULL) { | ||
peytondmurray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return -1; | ||
} | ||
|
||
out->buf = buf; | ||
out->len = num_bytes; | ||
|
||
return 0; | ||
} | ||
|
||
static ss EMPTY = {0, "\0"}; | ||
|
||
void | ||
load_string(char *data, ss **out) | ||
{ | ||
if (data == NULL) { | ||
*out = ∅ | ||
} | ||
else { | ||
*out = (ss *)data; | ||
} | ||
ngoldbaum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,21 +6,40 @@ | |
|
||
typedef struct ss { | ||
size_t len; | ||
char buf[]; | ||
char *buf; | ||
} ss; | ||
|
||
// allocates a new ss string of length len, filling with the contents of init | ||
ss * | ||
ssnewlen(const char *init, size_t len); | ||
// Allocates a new buffer for *to_init*, filling with the copied contents of | ||
// *init* and sets *to_init->len* to *len*. | ||
int | ||
ssnewlen(const char *init, size_t len, ss *to_init); | ||
|
||
// returns a new heap-allocated copy of input string *s* | ||
ss * | ||
ssdup(const ss *s); | ||
// Frees the internal string buffer held by *str*. If str->buf is NULL, does | ||
// nothing. | ||
void | ||
ssfree(ss *str); | ||
|
||
// returns a new, empty string of length len | ||
// does not do any initialization, the caller must | ||
// initialize and null-terminate the string | ||
ss * | ||
ssnewempty(size_t len); | ||
// copies the contents out *in* into *out*. Allocates a new string buffer for | ||
// *out*, checks that *out->buf* is NULL and *out->len* is zero and errors | ||
// otherwise. Also errors if malloc fails. | ||
int | ||
ssdup(ss *in, ss *out); | ||
|
||
// Allocates a new string buffer for *out* with enough capacity to store | ||
// *num_bytes* of text. The actual allocation is num_bytes + 1 bytes, to | ||
// account for the null terminator. Does not do any initialization, the caller | ||
// must initialize and null-terminate the string buffer. Errors if *out* | ||
// already contains data or if malloc fails. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚀 This is super valuable documentation. Thank you! |
||
int | ||
ssnewemptylen(size_t num_bytes, ss *out); | ||
|
||
// Interpret the contents of buffer *data* as an ss struct and set *out* to | ||
// that struct. If *data* is NULL, set *out* to point to a statically | ||
// allocated, empty SS struct. Since this function may set *out* to point to | ||
// statically allocated data, do not ever free memory owned by an output of | ||
// this function. That means this function is most useful for read-only | ||
// applications. | ||
void | ||
load_string(char *data, ss **out); | ||
|
||
#endif /*_NPY_STATIC_STRING_H */ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.