Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/sort/bwstring.h
Show All 39 Lines | |||||||||||||||
#include "mem.h" | #include "mem.h" | ||||||||||||||
extern bool byte_sort; | extern bool byte_sort; | ||||||||||||||
/* wchar_t is of 4 bytes: */ | /* wchar_t is of 4 bytes: */ | ||||||||||||||
#define SIZEOF_WCHAR_STRING(LEN) ((LEN)*sizeof(wchar_t)) | #define SIZEOF_WCHAR_STRING(LEN) ((LEN)*sizeof(wchar_t)) | ||||||||||||||
struct wstr { | |||||||||||||||
size_t len; | |||||||||||||||
wchar_t str[]; | |||||||||||||||
}; | |||||||||||||||
struct cstr { | |||||||||||||||
size_t len; | |||||||||||||||
char str[]; | |||||||||||||||
}; | |||||||||||||||
/* | /* | ||||||||||||||
* Binary "wide" string | * Binary "wide" string | ||||||||||||||
*/ | */ | ||||||||||||||
struct bwstring | struct bwstring | ||||||||||||||
{ | { | ||||||||||||||
size_t len; | union { | ||||||||||||||
union | struct wstr wdata; | ||||||||||||||
{ | struct cstr cdata; | ||||||||||||||
wchar_t wstr[0]; | |||||||||||||||
unsigned char cstr[0]; | |||||||||||||||
} data; | |||||||||||||||
}; | }; | ||||||||||||||
}; | |||||||||||||||
struct reader_buffer | struct reader_buffer | ||||||||||||||
{ | { | ||||||||||||||
wchar_t *fgetwln_z_buffer; | wchar_t *fgetwln_z_buffer; | ||||||||||||||
size_t fgetwln_z_buffer_size; | size_t fgetwln_z_buffer_size; | ||||||||||||||
}; | }; | ||||||||||||||
arichardson: This field is needed since otherwise the compiler complains: https://godbolt.org/z/TqeEcn… | |||||||||||||||
Not Done Inline ActionsWould a nice but more invasive solution be to define two named structures containing wstr, and cstr, and make bwstring a union of the two? markj: Would a nice but more invasive solution be to define two named structures containing wstr, and… | |||||||||||||||
Not Done Inline ActionsI would say so. There are "only" 57 instances of ->len (and no .len) so it wouldn't be _that_ painful to fix if someone felt sufficiently motivated. jrtc27: I would say so. There are "only" 57 instances of `->len` (and no `.len`) so it wouldn't be… | |||||||||||||||
Done Inline ActionsI agree it would be much nicer, maybe it's not too difficult to update with a bit of search+replace arichardson: I agree it would be much nicer, maybe it's not too difficult to update with a bit of… | |||||||||||||||
typedef void *bwstring_iterator; | typedef void *bwstring_iterator; | ||||||||||||||
#define BWSLEN(s) ((s)->len) | #define BWSLEN(s) ((MB_CUR_MAX == 1) ? (s)->cdata.len : (s)->wdata.len) | ||||||||||||||
struct bwstring *bwsalloc(size_t sz); | struct bwstring *bwsalloc(size_t sz); | ||||||||||||||
size_t bwsrawlen(const struct bwstring *bws); | size_t bwsrawlen(const struct bwstring *bws); | ||||||||||||||
const void* bwsrawdata(const struct bwstring *bws); | const void* bwsrawdata(const struct bwstring *bws); | ||||||||||||||
void bws_setlen(struct bwstring *bws, size_t newlen); | void bws_setlen(struct bwstring *bws, size_t newlen); | ||||||||||||||
size_t bws_memsize(const struct bwstring *bws); | size_t bws_memsize(const struct bwstring *bws); | ||||||||||||||
double bwstod(struct bwstring *s0, bool *empty); | double bwstod(struct bwstring *s0, bool *empty); | ||||||||||||||
int bws_month_score(const struct bwstring *s0); | int bws_month_score(const struct bwstring *s0); | ||||||||||||||
Show All 18 Lines | |||||||||||||||
int bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset); | int bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset); | ||||||||||||||
size_t bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended); | size_t bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended); | ||||||||||||||
struct bwstring *bwsfgetln(FILE *file, size_t *len, bool zero_ended, struct reader_buffer *rb); | struct bwstring *bwsfgetln(FILE *file, size_t *len, bool zero_ended, struct reader_buffer *rb); | ||||||||||||||
static inline bwstring_iterator | static inline bwstring_iterator | ||||||||||||||
bws_begin(struct bwstring *bws) | bws_begin(struct bwstring *bws) | ||||||||||||||
{ | { | ||||||||||||||
return (bwstring_iterator) (&(bws->data)); | return ((bwstring_iterator)bws->wdata.str); | ||||||||||||||
arichardsonAuthorUnsubmitted Done Inline Actions
Should fix accesses the non-active union member arichardson: Should fix accesses the non-active union member | |||||||||||||||
} | } | ||||||||||||||
static inline bwstring_iterator | static inline bwstring_iterator | ||||||||||||||
bws_end(struct bwstring *bws) | bws_end(struct bwstring *bws) | ||||||||||||||
{ | { | ||||||||||||||
return ((MB_CUR_MAX == 1) ? | return ((MB_CUR_MAX == 1) ? | ||||||||||||||
(bwstring_iterator) (bws->data.cstr + bws->len) : | (bwstring_iterator) (bws->cdata.str + bws->cdata.len) : | ||||||||||||||
(bwstring_iterator) (bws->data.wstr + bws->len)); | (bwstring_iterator) (bws->wdata.str + bws->wdata.len)); | ||||||||||||||
} | } | ||||||||||||||
static inline bwstring_iterator | static inline bwstring_iterator | ||||||||||||||
bws_iterator_inc(bwstring_iterator iter, size_t pos) | bws_iterator_inc(bwstring_iterator iter, size_t pos) | ||||||||||||||
{ | { | ||||||||||||||
if (MB_CUR_MAX == 1) | if (MB_CUR_MAX == 1) | ||||||||||||||
return ((unsigned char *) iter) + pos; | return ((unsigned char *) iter) + pos; | ||||||||||||||
Show All 9 Lines | if (MB_CUR_MAX == 1) | ||||||||||||||
return *((unsigned char *) iter); | return *((unsigned char *) iter); | ||||||||||||||
else | else | ||||||||||||||
return *((wchar_t*) iter); | return *((wchar_t*) iter); | ||||||||||||||
} | } | ||||||||||||||
int | int | ||||||||||||||
bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len); | bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len); | ||||||||||||||
#define BWS_GET(bws, pos) ((MB_CUR_MAX == 1) ? ((bws)->data.cstr[(pos)]) : (bws)->data.wstr[(pos)]) | #define BWS_GET(bws, pos) ((MB_CUR_MAX == 1) ? (bws->cdata.str[(pos)]) : bws->wdata.str[(pos)]) | ||||||||||||||
void initialise_months(void); | void initialise_months(void); | ||||||||||||||
#endif /* __BWSTRING_H__ */ | #endif /* __BWSTRING_H__ */ |
This field is needed since otherwise the compiler complains: https://godbolt.org/z/TqeEcn
flexible array member 'wstr' not allowed in otherwise empty struct