@@ -543,13 +543,15 @@ static const char *clearbuff (BuffFS *buff) {
543543}
544544
545545
546- static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
546+ // Phase 115.1: Updated to use std::span
547+ static void addstr2buff (BuffFS *buff, std::span<const char > str) {
548+ size_t slen = str.size ();
547549 size_t left = buff->buffsize - buff->blen ; /* space left in the buffer */
548550 if (buff->err ) /* do nothing else after an error */
549551 return ;
550552 if (slen > left) { /* new string doesn't fit into current buffer? */
551553 if (slen > ((MAX_SIZE/2 ) - buff->blen )) { /* overflow? */
552- memcpy (buff->b + buff->blen , str, left); /* copy what it can */
554+ memcpy (buff->b + buff->blen , str. data () , left); /* copy what it can */
553555 buff->blen = buff->buffsize ;
554556 buff->err = 2 ; /* doesn't add anything else */
555557 return ;
@@ -571,7 +573,7 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
571573 buff->buffsize = newsize; /* ...and its new size */
572574 }
573575 }
574- memcpy (buff->b + buff->blen , str, slen); /* copy new content */
576+ memcpy (buff->b + buff->blen , str. data () , slen); /* copy new content */
575577 buff->blen += slen;
576578}
577579
@@ -582,7 +584,7 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
582584static void addnum2buff (BuffFS *buff, TValue *num) {
583585 char numbuff[LUA_N2SBUFFSZ];
584586 unsigned len = luaO_tostringbuff (num, numbuff);
585- addstr2buff (buff, numbuff, len);
587+ addstr2buff (buff, std::span ( numbuff, len) );
586588}
587589
588590
@@ -594,17 +596,17 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
594596 const char *e; /* points to next '%' */
595597 BuffFS buff (L); /* holds last part of the result */
596598 while ((e = strchr (fmt, ' %' )) != nullptr ) {
597- addstr2buff (&buff, fmt, ct_diff2sz (e - fmt)); /* add 'fmt' up to '%' */
599+ addstr2buff (&buff, std::span ( fmt, ct_diff2sz (e - fmt) )); /* add 'fmt' up to '%' */
598600 switch (*(e + 1 )) { /* conversion specifier */
599601 case ' s' : { /* zero-terminated string */
600602 const char *s = va_arg (argp, char *);
601603 if (s == nullptr ) s = " (null)" ;
602- addstr2buff (&buff, s, strlen (s));
604+ addstr2buff (&buff, std::span ( s, strlen (s) ));
603605 break ;
604606 }
605607 case ' c' : { /* an 'int' as a character */
606608 char c = cast_char (va_arg (argp, int ));
607- addstr2buff (&buff, &c, sizeof ( char ));
609+ addstr2buff (&buff, std::span ( &c, 1 ));
608610 break ;
609611 }
610612 case ' d' : { /* an 'int' */
@@ -629,28 +631,28 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
629631 char bf[LUA_N2SBUFFSZ]; /* enough space for '%p' */
630632 void *p = va_arg (argp, void *);
631633 int len = lua_pointer2str (bf, LUA_N2SBUFFSZ, p);
632- addstr2buff (&buff, bf, cast_uint (len));
634+ addstr2buff (&buff, std::span ( bf, cast_uint (len) ));
633635 break ;
634636 }
635637 case ' U' : { /* an 'unsigned long' as a UTF-8 sequence */
636638 char bf[UTF8BUFFSZ];
637639 unsigned long arg = va_arg (argp, unsigned long );
638640 int len = luaO_utf8esc (bf, static_cast <l_uint32>(arg));
639- addstr2buff (&buff, bf + UTF8BUFFSZ - len, cast_uint (len));
641+ addstr2buff (&buff, std::span ( bf + UTF8BUFFSZ - len, cast_uint (len) ));
640642 break ;
641643 }
642644 case ' %' : {
643- addstr2buff (&buff, " %" , 1 );
645+ addstr2buff (&buff, std::span ( " %" , 1 ) );
644646 break ;
645647 }
646648 default : {
647- addstr2buff (&buff, e, 2 ); /* keep unknown format in the result */
649+ addstr2buff (&buff, std::span ( e, 2 ) ); /* keep unknown format in the result */
648650 break ;
649651 }
650652 }
651653 fmt = e + 2 ; /* skip '%' and the specifier */
652654 }
653- addstr2buff (&buff, fmt, strlen (fmt)); /* rest of 'fmt' */
655+ addstr2buff (&buff, std::span ( fmt, strlen (fmt) )); /* rest of 'fmt' */
654656 return clearbuff (&buff); /* empty buffer into a new string */
655657}
656658
@@ -673,45 +675,50 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
673675#define PRE " [string \" "
674676#define POS " \" ]"
675677
676- inline void addstr (char *& a, const char * b, size_t l) noexcept {
677- std::copy_n (b, l, a);
678- a += l;
678+ // Phase 115.1: Updated to use std::span
679+ inline void addstr (std::span<char >& dest, std::span<const char > src) noexcept {
680+ std::copy (src.begin (), src.end (), dest.begin ());
681+ dest = dest.subspan (src.size ());
679682}
680683
681- void luaO_chunkid (char *out, const char *source, size_t srclen) {
682- size_t bufflen = LUA_IDSIZE; /* free space in buffer */
683- if (*source == ' =' ) { /* 'literal' source */
684- if (srclen <= bufflen) /* small enough? */
685- std::copy_n (source + 1 , srclen, out);
684+ // Phase 115.1: std::span-based chunk ID formatting
685+ void luaO_chunkid (std::span<char > out, std::span<const char > source) {
686+ size_t bufflen = out.size (); /* free space in buffer */
687+ size_t srclen = source.size ();
688+ if (!source.empty () && source[0 ] == ' =' ) { /* 'literal' source */
689+ if (srclen <= bufflen) { /* small enough? */
690+ std::copy_n (source.data () + 1 , srclen, out.data ());
691+ }
686692 else { /* truncate it */
687- addstr (out, source + 1 , bufflen - 1 );
688- * out = ' \0 ' ;
693+ addstr (out, source. subspan ( 1 , bufflen - 1 ) );
694+ out[ 0 ] = ' \0 ' ;
689695 }
690696 }
691- else if (*source == ' @' ) { /* file name */
692- if (srclen <= bufflen) /* small enough? */
693- std::copy_n (source + 1 , srclen, out);
697+ else if (!source.empty () && source[0 ] == ' @' ) { /* file name */
698+ if (srclen <= bufflen) { /* small enough? */
699+ std::copy_n (source.data () + 1 , srclen, out.data ());
700+ }
694701 else { /* add '...' before rest of name */
695- addstr (out, RETS, LL (RETS));
702+ addstr (out, std::span ( RETS, LL (RETS) ));
696703 bufflen -= LL (RETS);
697- std::copy_n (source + 1 + srclen - bufflen, bufflen, out);
704+ std::copy_n (source. data () + 1 + srclen - bufflen, bufflen, out. data () );
698705 }
699706 }
700707 else { /* string; format as [string "source"] */
701- const char *nl = strchr (source, ' \n ' ); /* find first new line (if any) */
702- addstr (out, PRE, LL (PRE)); /* add prefix */
708+ const char *nl = strchr (source. data () , ' \n ' ); /* find first new line (if any) */
709+ addstr (out, std::span ( PRE, LL (PRE) )); /* add prefix */
703710 bufflen -= LL (PRE RETS POS) + 1 ; /* save space for prefix+suffix+'\0' */
704711 if (srclen < bufflen && nl == nullptr ) { /* small one-line source? */
705- addstr (out, source, srclen ); /* keep it */
712+ addstr (out, source); /* keep it */
706713 }
707714 else {
708715 if (nl != nullptr )
709- srclen = ct_diff2sz (nl - source); /* stop at first newline */
716+ srclen = ct_diff2sz (nl - source. data () ); /* stop at first newline */
710717 if (srclen > bufflen) srclen = bufflen;
711- addstr (out, source, srclen);
712- addstr (out, RETS, LL (RETS));
718+ addstr (out, source. subspan ( 0 , srclen) );
719+ addstr (out, std::span ( RETS, LL (RETS) ));
713720 }
714- std::copy_n (POS, LL (POS) + 1 , out);
721+ std::copy_n (POS, LL (POS) + 1 , out. data () );
715722 }
716723}
717724
0 commit comments