Berry add global function `format` as a simpler syntax to `string.format` (#18925)

This commit is contained in:
s-hadinger 2023-06-21 14:12:39 +02:00 committed by GitHub
parent 635e240293
commit 410aadbf6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 3 deletions

View File

@ -14,6 +14,7 @@
#include "be_vector.h" #include "be_vector.h"
#include "be_string.h" #include "be_string.h"
#include "be_map.h" #include "be_map.h"
#include "be_strlib.h"
#include <string.h> #include <string.h>
#define READLINE_STEP 100 #define READLINE_STEP 100
@ -504,6 +505,7 @@ void be_load_baselib_next(bvm *vm)
{ {
be_regfunc(vm, "call", l_call); be_regfunc(vm, "call", l_call);
be_regfunc(vm, "bool", l_bool); be_regfunc(vm, "bool", l_bool);
be_regfunc(vm, "format", be_str_format);
} }
#else #else
extern const bclass be_class_list; extern const bclass be_class_list;
@ -537,6 +539,7 @@ vartab m_builtin (scope: local) {
bytes, class(be_class_bytes) bytes, class(be_class_bytes)
call, func(l_call) call, func(l_call)
bool, func(l_bool) bool, func(l_bool)
format, func(be_str_format)
} }
@const_object_info_end */ @const_object_info_end */
#include "../generate/be_fixed_m_builtin.h" #include "../generate/be_fixed_m_builtin.h"

View File

@ -599,7 +599,7 @@ static bbool convert_to_real(bvm *vm, int index, breal *val)
return converted; return converted;
} }
static int str_format(bvm *vm) int be_str_format(bvm *vm)
{ {
int top = be_top(vm); int top = be_top(vm);
if (top > 0 && be_isstring(vm, 1)) { if (top > 0 && be_isstring(vm, 1)) {
@ -940,7 +940,7 @@ static int str_escape(bvm *vm)
#if !BE_USE_PRECOMPILED_OBJECT #if !BE_USE_PRECOMPILED_OBJECT
be_native_module_attr_table(string) { be_native_module_attr_table(string) {
be_native_module_function("format", str_format), be_native_module_function("format", be_str_format),
be_native_module_function("count", str_count), be_native_module_function("count", str_count),
be_native_module_function("split", str_split), be_native_module_function("split", str_split),
be_native_module_function("find", str_find), be_native_module_function("find", str_find),
@ -958,7 +958,7 @@ be_define_native_module(string, NULL);
#else #else
/* @const_object_info_begin /* @const_object_info_begin
module string (scope: global, depend: BE_USE_STRING_MODULE) { module string (scope: global, depend: BE_USE_STRING_MODULE) {
format, func(str_format) format, func(be_str_format)
count, func(str_count) count, func(str_count)
split, func(str_split) split, func(str_split)
find, func(str_find) find, func(str_find)

View File

@ -25,6 +25,7 @@ const char* be_splitpath(const char *path);
const char* be_splitname(const char *path); const char* be_splitname(const char *path);
const char* be_pushvfstr(bvm *vm, const char *format, va_list arg); const char* be_pushvfstr(bvm *vm, const char *format, va_list arg);
bstring* be_strindex(bvm *vm, bstring *str, bvalue *idx); bstring* be_strindex(bvm *vm, bstring *str, bvalue *idx);
int be_str_format(bvm *vm);
#ifdef __cplusplus #ifdef __cplusplus
} }