Add RedisModule_CreateStringFromString().

This commit is contained in:
Yossi Gottlieb 2016-06-22 20:57:24 +03:00
parent 4e10b08fb3
commit 61172ed01e
5 changed files with 26 additions and 2 deletions

View File

@ -673,6 +673,17 @@ RedisModuleString *RM_CreateStringFromLongLong(RedisModuleCtx *ctx, long long ll
return RM_CreateString(ctx,buf,len); return RM_CreateString(ctx,buf,len);
} }
/* Like RedisModule_CreatString(), but creates a string starting from another
* RedisModuleString.
*
* The returned string must be released with RedisModule_FreeString() or by
* enabling automatic memory management. */
RedisModuleString *RM_CreateStringFromString(RedisModuleCtx *ctx, const RedisModuleString *str) {
RedisModuleString *o = dupStringObject(str);
autoMemoryAdd(ctx,REDISMODULE_AM_STRING,o);
return o;
}
/* Free a module string object obtained with one of the Redis modules API calls /* Free a module string object obtained with one of the Redis modules API calls
* that return new string objects. * that return new string objects.
* *
@ -2828,6 +2839,7 @@ void moduleRegisterCoreAPI(void) {
REGISTER_API(CreateStringFromCallReply); REGISTER_API(CreateStringFromCallReply);
REGISTER_API(CreateString); REGISTER_API(CreateString);
REGISTER_API(CreateStringFromLongLong); REGISTER_API(CreateStringFromLongLong);
REGISTER_API(CreateStringFromString);
REGISTER_API(FreeString); REGISTER_API(FreeString);
REGISTER_API(StringPtrLen); REGISTER_API(StringPtrLen);
REGISTER_API(AutoMemory); REGISTER_API(AutoMemory);

View File

@ -179,6 +179,16 @@ integer instead of taking a buffer and its length.
The returned string must be released with `RedisModule_FreeString()` or by The returned string must be released with `RedisModule_FreeString()` or by
enabling automatic memory management. enabling automatic memory management.
## `RM_CreateStringFromString`
RedisModuleString *RM_CreateStringFromString(RedisModuleCtx *ctx, const RedisModuleString *str);
Like `RedisModule_CreatString()`, but creates a string starting from an existing
RedisModuleString.
The returned string must be released with `RedisModule_FreeString()` or by
enabling automatic memory management.
## `RM_FreeString` ## `RM_FreeString`
void RM_FreeString(RedisModuleCtx *ctx, RedisModuleString *str); void RM_FreeString(RedisModuleCtx *ctx, RedisModuleString *str);

View File

@ -147,7 +147,7 @@ robj *createStringObjectFromLongDouble(long double value, int humanfriendly) {
* will always result in a fresh object that is unshared (refcount == 1). * will always result in a fresh object that is unshared (refcount == 1).
* *
* The resulting object always has refcount set to 1. */ * The resulting object always has refcount set to 1. */
robj *dupStringObject(robj *o) { robj *dupStringObject(const robj *o) {
robj *d; robj *d;
serverAssert(o->type == OBJ_STRING); serverAssert(o->type == OBJ_STRING);

View File

@ -122,6 +122,7 @@ size_t REDISMODULE_API_FUNC(RedisModule_CallReplyLength)(RedisModuleCallReply *r
RedisModuleCallReply *REDISMODULE_API_FUNC(RedisModule_CallReplyArrayElement)(RedisModuleCallReply *reply, size_t idx); RedisModuleCallReply *REDISMODULE_API_FUNC(RedisModule_CallReplyArrayElement)(RedisModuleCallReply *reply, size_t idx);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateString)(RedisModuleCtx *ctx, const char *ptr, size_t len); RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateString)(RedisModuleCtx *ctx, const char *ptr, size_t len);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateStringFromLongLong)(RedisModuleCtx *ctx, long long ll); RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateStringFromLongLong)(RedisModuleCtx *ctx, long long ll);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateStringFromString)(RedisModuleCtx *ctx, const RedisModuleString *str);
void REDISMODULE_API_FUNC(RedisModule_FreeString)(RedisModuleCtx *ctx, RedisModuleString *str); void REDISMODULE_API_FUNC(RedisModule_FreeString)(RedisModuleCtx *ctx, RedisModuleString *str);
const char *REDISMODULE_API_FUNC(RedisModule_StringPtrLen)(RedisModuleString *str, size_t *len); const char *REDISMODULE_API_FUNC(RedisModule_StringPtrLen)(RedisModuleString *str, size_t *len);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithError)(RedisModuleCtx *ctx, const char *err); int REDISMODULE_API_FUNC(RedisModule_ReplyWithError)(RedisModuleCtx *ctx, const char *err);
@ -225,6 +226,7 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
REDISMODULE_GET_API(CreateStringFromCallReply); REDISMODULE_GET_API(CreateStringFromCallReply);
REDISMODULE_GET_API(CreateString); REDISMODULE_GET_API(CreateString);
REDISMODULE_GET_API(CreateStringFromLongLong); REDISMODULE_GET_API(CreateStringFromLongLong);
REDISMODULE_GET_API(CreateStringFromString);
REDISMODULE_GET_API(FreeString); REDISMODULE_GET_API(FreeString);
REDISMODULE_GET_API(StringPtrLen); REDISMODULE_GET_API(StringPtrLen);
REDISMODULE_GET_API(AutoMemory); REDISMODULE_GET_API(AutoMemory);

View File

@ -1285,7 +1285,7 @@ robj *createObject(int type, void *ptr);
robj *createStringObject(const char *ptr, size_t len); robj *createStringObject(const char *ptr, size_t len);
robj *createRawStringObject(const char *ptr, size_t len); robj *createRawStringObject(const char *ptr, size_t len);
robj *createEmbeddedStringObject(const char *ptr, size_t len); robj *createEmbeddedStringObject(const char *ptr, size_t len);
robj *dupStringObject(robj *o); robj *dupStringObject(const robj *o);
int isSdsRepresentableAsLongLong(sds s, long long *llval); int isSdsRepresentableAsLongLong(sds s, long long *llval);
int isObjectRepresentableAsLongLong(robj *o, long long *llongval); int isObjectRepresentableAsLongLong(robj *o, long long *llongval);
robj *tryObjectEncoding(robj *o); robj *tryObjectEncoding(robj *o);