cell_strdup -> nccell_strdup

This commit is contained in:
nick black 2021-04-10 09:21:52 -04:00
parent 94a2badc06
commit ff76dba6ed
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
14 changed files with 57 additions and 41 deletions

View File

@ -1859,20 +1859,20 @@ void nccell_release(struct ncplane* n, nccell* c);
// copy the UTF8-encoded EGC out of the cell, whether simple or complex. the // copy the UTF8-encoded EGC out of the cell, whether simple or complex. the
// result is not tied to the ncplane, and persists across erases / destruction. // result is not tied to the ncplane, and persists across erases / destruction.
static inline char* static inline char*
cell_strdup(const struct ncplane* n, const nccell* c){ nccell_strdup(const struct ncplane* n, const nccell* c){
return strdup(cell_extended_gcluster(n, c)); return strdup(cell_extended_gcluster(n, c));
} }
// Set the specified style bits for the cell 'c', whether they're actively // Set the specified style bits for the cell 'c', whether they're actively
// supported or not. Only the lower 16 bits are meaningful. // supported or not. Only the lower 16 bits are meaningful.
static inline void static inline void
cell_styles_set(nccell* c, unsigned stylebits){ nccell_styles_set(nccell* c, unsigned stylebits){
c->stylemask = stylebits & NCSTYLE_MASK; c->stylemask = stylebits & NCSTYLE_MASK;
} }
// Extract the style bits from the cell. // Extract the style bits from the cell.
static inline unsigned static inline unsigned
cell_styles(const nccell* c){ nccell_styles(const nccell* c){
return c->stylemask; return c->stylemask;
} }

View File

@ -51,9 +51,9 @@ typedef struct nccell {
**void nccell_release(struct ncplane* ***n***, nccell* ***c***);** **void nccell_release(struct ncplane* ***n***, nccell* ***c***);**
**void cell_styles_set(nccell* ***c***, unsigned ***stylebits***);** **void nccell_styles_set(nccell* ***c***, unsigned ***stylebits***);**
**unsigned cell_styles(const nccell* ***c***);** **unsigned nccell_styles(const nccell* ***c***);**
**void cell_on_styles(nccell* ***c***, unsigned ***stylebits***);** **void cell_on_styles(nccell* ***c***, unsigned ***stylebits***);**
@ -71,7 +71,7 @@ typedef struct nccell {
**const char* cell_extended_gcluster(const struct ncplane* ***n***, const nccell* ***c***);** **const char* cell_extended_gcluster(const struct ncplane* ***n***, const nccell* ***c***);**
**char* cell_strdup(const struct ncplane* ***n***, const nccell* ***c***);** **char* nccell_strdup(const struct ncplane* ***n***, const nccell* ***c***);**
**int cell_load_char(struct ncplane* ***n***, nccell* ***c***, char ***ch***);** **int cell_load_char(struct ncplane* ***n***, nccell* ***c***, char ***ch***);**
@ -141,7 +141,7 @@ release **nccell**s can eventually block new output.
**cell_extended_gcluster** provides a nul-terminated handle to the EGC. This **cell_extended_gcluster** provides a nul-terminated handle to the EGC. This
ought be considered invalidated by changes to the **nccell** or **egcpool**. ought be considered invalidated by changes to the **nccell** or **egcpool**.
The handle is **not** heap-allocated; do **not** attempt to **free(3)** it. The handle is **not** heap-allocated; do **not** attempt to **free(3)** it.
A heap-allocated copy can be acquired with **cell_strdup**. A heap-allocated copy can be acquired with **nccell_strdup**.
# RETURN VALUES # RETURN VALUES

View File

@ -77,12 +77,12 @@ namespace ncpp
void set_styles (CellStyle styles) noexcept void set_styles (CellStyle styles) noexcept
{ {
cell_set_styles (&_cell, static_cast<unsigned>(styles)); nccell_set_styles (&_cell, static_cast<unsigned>(styles));
} }
CellStyle get_styles () noexcept CellStyle get_styles () noexcept
{ {
return static_cast<CellStyle>(cell_styles (&_cell)); return static_cast<CellStyle>(nccell_styles (&_cell));
} }
void styles_on (CellStyle styles) noexcept void styles_on (CellStyle styles) noexcept

View File

@ -1193,7 +1193,7 @@ namespace ncpp
char* strdup (Cell const& cell) const noexcept char* strdup (Cell const& cell) const noexcept
{ {
return cell_strdup (plane, cell); return nccell_strdup (plane, cell);
} }
char* extract (Cell const& cell, uint16_t *stylemask = nullptr, uint64_t *channels = nullptr) char* extract (Cell const& cell, uint16_t *stylemask = nullptr, uint64_t *channels = nullptr)

View File

@ -693,16 +693,27 @@ cell_release(struct ncplane* n, nccell* c){
// Set the specified style bits for the nccell 'c', whether they're actively // Set the specified style bits for the nccell 'c', whether they're actively
// supported or not. Only the lower 16 bits are meaningful. // supported or not. Only the lower 16 bits are meaningful.
static inline void static inline void
cell_set_styles(nccell* c, unsigned stylebits){ nccell_set_styles(nccell* c, unsigned stylebits){
c->stylemask = stylebits & NCSTYLE_MASK; c->stylemask = stylebits & NCSTYLE_MASK;
} }
__attribute__ ((deprecated)) static inline void
cell_set_styles(nccell* c, unsigned stylebits){
nccell_set_styles(c, stylebits);
}
// Extract the style bits from the nccell. // Extract the style bits from the nccell.
static inline unsigned static inline unsigned
cell_styles(const nccell* c){ nccell_styles(const nccell* c){
return c->stylemask; return c->stylemask;
} }
// Extract the style bits from the nccell.
__attribute__ ((deprecated)) static inline unsigned
cell_styles(const nccell* c){
return nccell_styles(c);
}
// Add the specified styles (in the LSBs) to the nccell's existing spec, // Add the specified styles (in the LSBs) to the nccell's existing spec,
// whether they're actively supported or not. // whether they're actively supported or not.
static inline void static inline void
@ -764,10 +775,15 @@ API const char* cell_extended_gcluster(const struct ncplane* n, const nccell* c)
// copy the UTF8-encoded EGC out of the nccell. the result is not tied to any // copy the UTF8-encoded EGC out of the nccell. the result is not tied to any
// ncplane, and persists across erases / destruction. // ncplane, and persists across erases / destruction.
ALLOC static inline char* ALLOC static inline char*
cell_strdup(const struct ncplane* n, const nccell* c){ nccell_strdup(const struct ncplane* n, const nccell* c){
return strdup(cell_extended_gcluster(n, c)); return strdup(cell_extended_gcluster(n, c));
} }
__attribute__ ((deprecated)) ALLOC static inline char*
cell_strdup(const struct ncplane* n, const nccell* c){
return nccell_strdup(n, c);
}
// Extract the three elements of a nccell. // Extract the three elements of a nccell.
static inline char* static inline char*
cell_extract(const struct ncplane* n, const nccell* c, cell_extract(const struct ncplane* n, const nccell* c,
@ -778,7 +794,7 @@ cell_extract(const struct ncplane* n, const nccell* c,
if(channels){ if(channels){
*channels = c->channels; *channels = c->channels;
} }
return cell_strdup(n, c); return nccell_strdup(n, c);
} }
// Returns true if the two nccells are distinct EGCs, attributes, or channels. // Returns true if the two nccells are distinct EGCs, attributes, or channels.

View File

@ -371,9 +371,9 @@ impl NcCell {
/// The result is not tied to the [NcPlane], /// The result is not tied to the [NcPlane],
/// and persists across erases and destruction. /// and persists across erases and destruction.
/// ///
/// *C style function: [cell_strdup()][crate::cell_strdup].* /// *C style function: [nccell_strdup()][crate::nccell_strdup].*
pub fn strdup(&self, plane: &NcPlane) -> NcEgc { pub fn strdup(&self, plane: &NcPlane) -> NcEgc {
crate::cell_strdup(plane, self) crate::nccell_strdup(plane, self)
} }
/// Does this NcCell contain a wide codepoint? /// Does this NcCell contain a wide codepoint?

View File

@ -5,12 +5,12 @@
// (W) wrap: 4 // (W) wrap: 4
// (#) test: 0 // (#) test: 0
// ------------------------------------------ // ------------------------------------------
//W cell_duplicate
//… cell_extended_gcluster //… cell_extended_gcluster
//… cell_load //… cell_load
//W cell_release
//W cells_double_box //W cells_double_box
//W cells_rounded_box //W cells_rounded_box
//W nccell_duplicate
//W nccell_release
// //
// functions manually reimplemented: 43 // functions manually reimplemented: 43
// ------------------------------------------ // ------------------------------------------
@ -57,10 +57,10 @@
// X cell_set_fg_rgb8_clipped // unneeded // X cell_set_fg_rgb8_clipped // unneeded
//W+ cell_set_styles //W+ cell_set_styles
//W+ cells_load_box //W+ cells_load_box
//W+ cell_strdup
//W+ cell_styles //W+ cell_styles
//W+ cell_wide_left_p //W+ cell_wide_left_p
//W+ cell_wide_right_p //W+ cell_wide_right_p
//W+ nccell_strdup
#[cfg(test)] #[cfg(test)]
mod test; mod test;

View File

@ -3,7 +3,7 @@
use libc::strcmp; use libc::strcmp;
use crate::{ use crate::{
cell_release, cstring, NcAlphaBits, NcCell, NcChannel, NcChannelPair, NcColor, NcEgc, nccell_release, cstring, NcAlphaBits, NcCell, NcChannel, NcChannelPair, NcColor, NcEgc,
NcIntResult, NcPaletteIndex, NcPlane, NcRgb, NcStyleMask, NCCELL_ALPHA_OPAQUE, NcIntResult, NcPaletteIndex, NcPlane, NcRgb, NcStyleMask, NCCELL_ALPHA_OPAQUE,
NCCELL_BGDEFAULT_MASK, NCCELL_BG_PALETTE, NCCELL_FGDEFAULT_MASK, NCCELL_FG_PALETTE, NCCELL_BGDEFAULT_MASK, NCCELL_BG_PALETTE, NCCELL_FGDEFAULT_MASK, NCCELL_FG_PALETTE,
NCRESULT_ERR, NCRESULT_OK, NCSTYLE_MASK, NCRESULT_ERR, NCRESULT_OK, NCSTYLE_MASK,
@ -348,7 +348,7 @@ pub const fn cell_wide_left_p(cell: &NcCell) -> bool {
/// ///
/// *Method: NcCell.[strdup()][NcCell#method.strdup].* /// *Method: NcCell.[strdup()][NcCell#method.strdup].*
#[inline] #[inline]
pub fn cell_strdup(plane: &NcPlane, cell: &NcCell) -> NcEgc { pub fn nccell_strdup(plane: &NcPlane, cell: &NcCell) -> NcEgc {
core::char::from_u32( core::char::from_u32(
unsafe { libc::strdup(crate::cell_extended_gcluster(plane, cell)) } as i32 as u32, unsafe { libc::strdup(crate::cell_extended_gcluster(plane, cell)) } as i32 as u32,
) )
@ -379,7 +379,7 @@ pub fn cell_extract(
if *channels != 0 { if *channels != 0 {
*channels = cell.channels; *channels = cell.channels;
} }
cell_strdup(plane, cell) nccell_strdup(plane, cell)
} }
/// Returns true if the two cells are distinct [NcEgc]s, attributes, or channels. /// Returns true if the two cells are distinct [NcEgc]s, attributes, or channels.
@ -440,7 +440,7 @@ pub fn cell_prime(
/// Returns [NCRESULT_OK] on success or [NCRESULT_ERR] on error. /// Returns [NCRESULT_OK] on success or [NCRESULT_ERR] on error.
/// ///
/// On error, any [NcCell]s this function might have loaded before the error /// On error, any [NcCell]s this function might have loaded before the error
/// are [cell_release]d. There must be at least six [NcEgc]s in `gcluster`. /// are [nccell_release]d. There must be at least six [NcEgc]s in `gcluster`.
/// ///
/// *Method: NcCell.[load_box()][NcCell#method.load_box].* /// *Method: NcCell.[load_box()][NcCell#method.load_box].*
pub fn cells_load_box( pub fn cells_load_box(
@ -486,23 +486,23 @@ pub fn cells_load_box(
return NCRESULT_OK; return NCRESULT_OK;
} }
unsafe { unsafe {
cell_release(plane, hl); nccell_release(plane, hl);
} }
} }
unsafe { unsafe {
cell_release(plane, lr); nccell_release(plane, lr);
} }
} }
unsafe { unsafe {
cell_release(plane, ll); nccell_release(plane, ll);
} }
} }
unsafe { unsafe {
cell_release(plane, ur); nccell_release(plane, ur);
} }
} }
unsafe { unsafe {
cell_release(plane, ul); nccell_release(plane, ul);
} }
} }
NCRESULT_ERR NCRESULT_ERR

View File

@ -450,7 +450,7 @@ infoplane_notcurses(struct notcurses* nc, const fetched_info* fi, int planeheigh
if(notcurses_cantruecolor(nc)){ if(notcurses_cantruecolor(nc)){
ncplane_printf_aligned(infop, 4, NCALIGN_LEFT, " RGB TERM: %s", fi->term); ncplane_printf_aligned(infop, 4, NCALIGN_LEFT, " RGB TERM: %s", fi->term);
nccell c = CELL_CHAR_INITIALIZER('R'); nccell c = CELL_CHAR_INITIALIZER('R');
cell_set_styles(&c, NCSTYLE_BOLD); nccell_set_styles(&c, NCSTYLE_BOLD);
cell_set_fg_rgb8(&c, 0xf0, 0xa0, 0xa0); cell_set_fg_rgb8(&c, 0xf0, 0xa0, 0xa0);
ncplane_putc_yx(infop, 4, 1, &c); ncplane_putc_yx(infop, 4, 1, &c);
cell_load_char(infop, &c, 'G'); cell_load_char(infop, &c, 'G');
@ -459,7 +459,7 @@ infoplane_notcurses(struct notcurses* nc, const fetched_info* fi, int planeheigh
cell_load_char(infop, &c, 'B'); cell_load_char(infop, &c, 'B');
cell_set_fg_rgb8(&c, 0xa0, 0xa0, 0xf0); cell_set_fg_rgb8(&c, 0xa0, 0xa0, 0xf0);
ncplane_putc_yx(infop, 4, 3, &c); ncplane_putc_yx(infop, 4, 3, &c);
cell_set_styles(&c, NCSTYLE_NONE); nccell_set_styles(&c, NCSTYLE_NONE);
}else{ }else{
ncplane_printf_aligned(infop, 4, NCALIGN_LEFT, " TERM: %s", fi->term); ncplane_printf_aligned(infop, 4, NCALIGN_LEFT, " TERM: %s", fi->term);
} }
@ -487,9 +487,9 @@ infoplane_notcurses(struct notcurses* nc, const fetched_info* fi, int planeheigh
channels_set_fg_rgb8(&channels, 0, 0xff, 0); channels_set_fg_rgb8(&channels, 0, 0xff, 0);
ncplane_hline_interp(infop, &hl, planewidth / 2, ul.channels, channels); ncplane_hline_interp(infop, &hl, planewidth / 2, ul.channels, channels);
ncplane_hline_interp(infop, &hl, planewidth / 2, channels, ur.channels); ncplane_hline_interp(infop, &hl, planewidth / 2, channels, ur.channels);
cell_release(infop, &ul); cell_release(infop, &ur); nccell_release(infop, &ul); nccell_release(infop, &ur);
cell_release(infop, &ll); cell_release(infop, &lr); nccell_release(infop, &ll); nccell_release(infop, &lr);
cell_release(infop, &hl); cell_release(infop, &vl); nccell_release(infop, &hl); nccell_release(infop, &vl);
ncplane_set_fg_rgb8(infop, 0xff, 0xff, 0xff); ncplane_set_fg_rgb8(infop, 0xff, 0xff, 0xff);
ncplane_set_styles(infop, NCSTYLE_BOLD); ncplane_set_styles(infop, NCSTYLE_BOLD);
if(ncplane_printf_aligned(infop, 0, NCALIGN_CENTER, "[ %s@%s ]", if(ncplane_printf_aligned(infop, 0, NCALIGN_CENTER, "[ %s@%s ]",

View File

@ -1957,7 +1957,7 @@ void ncplane_erase(ncplane* n){
// we must preserve the background, but a pure nccell_duplicate() would be // we must preserve the background, but a pure nccell_duplicate() would be
// wiped out by the egcpool_dump(). do a duplication (to get the stylemask // wiped out by the egcpool_dump(). do a duplication (to get the stylemask
// and channels), and then reload. // and channels), and then reload.
char* egc = cell_strdup(n, &n->basecell); char* egc = nccell_strdup(n, &n->basecell);
memset(n->fb, 0, sizeof(*n->fb) * n->leny * n->lenx); memset(n->fb, 0, sizeof(*n->fb) * n->leny * n->lenx);
egcpool_dump(&n->pool); egcpool_dump(&n->pool);
egcpool_init(&n->pool); egcpool_init(&n->pool);

View File

@ -173,7 +173,7 @@ int redraw_plot_##T(nc##X##plot* ncp){ \
nccell* c = ncplane_cell_ref_yx(ncp->ncp, dimy - y - 1, x); \ nccell* c = ncplane_cell_ref_yx(ncp->ncp, dimy - y - 1, x); \
cell_set_bchannel(c, channels_bchannel(channels)); \ cell_set_bchannel(c, channels_bchannel(channels)); \
cell_set_fchannel(c, channels_fchannel(channels)); \ cell_set_fchannel(c, channels_fchannel(channels)); \
cell_set_styles(c, NCSTYLE_NONE); \ nccell_set_styles(c, NCSTYLE_NONE); \
if(pool_blit_direct(&ncp->ncp->pool, c, utf8, bytes, 1) <= 0){ \ if(pool_blit_direct(&ncp->ncp->pool, c, utf8, bytes, 1) <= 0){ \
return -1; \ return -1; \
} \ } \

View File

@ -592,7 +592,7 @@ int term_setstyle(FILE* out, unsigned cur, unsigned targ, unsigned stylebit,
static inline int static inline int
term_setstyles(FILE* out, notcurses* nc, const nccell* c){ term_setstyles(FILE* out, notcurses* nc, const nccell* c){
bool normalized = false; bool normalized = false;
uint32_t cellattr = cell_styles(c); uint32_t cellattr = nccell_styles(c);
if(cellattr == nc->rstate.curattr){ if(cellattr == nc->rstate.curattr){
return 0; // happy agreement, change nothing return 0; // happy agreement, change nothing
} }

View File

@ -71,7 +71,7 @@ TEST_CASE("Cell") {
nccell c = CELL_TRIVIAL_INITIALIZER; nccell c = CELL_TRIVIAL_INITIALIZER;
int dimy, dimx; int dimy, dimx;
notcurses_term_dim_yx(nc_, &dimy, &dimx); notcurses_term_dim_yx(nc_, &dimy, &dimx);
cell_set_styles(&c, NCSTYLE_ITALIC); nccell_set_styles(&c, NCSTYLE_ITALIC);
CHECK(1 == cell_load(n_, &c, "i")); CHECK(1 == cell_load(n_, &c, "i"));
cell_set_fg_rgb8(&c, 255, 255, 255); cell_set_fg_rgb8(&c, 255, 255, 255);
ncplane_set_base_cell(n_, &c); ncplane_set_base_cell(n_, &c);
@ -84,7 +84,7 @@ TEST_CASE("Cell") {
nccell c = CELL_TRIVIAL_INITIALIZER; nccell c = CELL_TRIVIAL_INITIALIZER;
int dimy, dimx; int dimy, dimx;
notcurses_term_dim_yx(nc_, &dimy, &dimx); notcurses_term_dim_yx(nc_, &dimy, &dimx);
cell_set_styles(&c, NCSTYLE_BOLD); nccell_set_styles(&c, NCSTYLE_BOLD);
CHECK(1 == cell_load(n_, &c, "b")); CHECK(1 == cell_load(n_, &c, "b"));
cell_set_fg_rgb8(&c, 255, 255, 255); cell_set_fg_rgb8(&c, 255, 255, 255);
ncplane_set_base_cell(n_, &c); ncplane_set_base_cell(n_, &c);
@ -97,7 +97,7 @@ TEST_CASE("Cell") {
nccell c = CELL_TRIVIAL_INITIALIZER; nccell c = CELL_TRIVIAL_INITIALIZER;
int dimy, dimx; int dimy, dimx;
notcurses_term_dim_yx(nc_, &dimy, &dimx); notcurses_term_dim_yx(nc_, &dimy, &dimx);
cell_set_styles(&c, NCSTYLE_UNDERLINE); nccell_set_styles(&c, NCSTYLE_UNDERLINE);
CHECK(1 == cell_load(n_, &c, "u")); CHECK(1 == cell_load(n_, &c, "u"));
cell_set_fg_rgb8(&c, 255, 255, 255); cell_set_fg_rgb8(&c, 255, 255, 255);
ncplane_set_base_cell(n_, &c); ncplane_set_base_cell(n_, &c);

View File

@ -503,7 +503,7 @@ TEST_CASE("Fills") {
for(int x = 0 ; x < DIMX ; ++x){ for(int x = 0 ; x < DIMX ; ++x){
CHECK(0 < ncplane_at_yx_cell(p1, y, x, &c1)); CHECK(0 < ncplane_at_yx_cell(p1, y, x, &c1));
if(y < 1 || y > 5 || x < 1 || x > 5){ if(y < 1 || y > 5 || x < 1 || x > 5){
auto cstr = cell_strdup(p1, &c1); auto cstr = nccell_strdup(p1, &c1);
CHECK(0 == strcmp(cstr, "")); CHECK(0 == strcmp(cstr, ""));
free(cstr); free(cstr);
}else{ }else{
@ -561,7 +561,7 @@ TEST_CASE("Fills") {
nccell c1 = CELL_TRIVIAL_INITIALIZER; nccell c1 = CELL_TRIVIAL_INITIALIZER;
CHECK(0 < ncplane_at_yx_cell(p1, y, x, &c1)); CHECK(0 < ncplane_at_yx_cell(p1, y, x, &c1));
if(y < 1 || y > 5 || x < 1 || x > 5){ if(y < 1 || y > 5 || x < 1 || x > 5){
auto cstr = cell_strdup(p1, &c1); auto cstr = nccell_strdup(p1, &c1);
CHECK(0 == strcmp(cstr, "")); CHECK(0 == strcmp(cstr, ""));
free(cstr); free(cstr);
}else{ }else{