Simplify xmpp_jid_{bare,domain,resource}

This commit is contained in:
Alexander Krotov
2017-06-23 19:55:12 +03:00
parent f51b01a5b3
commit b957213fdc

View File

@@ -72,15 +72,13 @@ char *xmpp_jid_new(xmpp_ctx_t *ctx, const char *node,
char *xmpp_jid_bare(xmpp_ctx_t *ctx, const char *jid) char *xmpp_jid_bare(xmpp_ctx_t *ctx, const char *jid)
{ {
char *result; char *result;
const char *c; size_t len;
c = strchr(jid, '/'); len = strcspn(jid, "/");
if (c == NULL) return xmpp_strdup(ctx, jid); result = xmpp_alloc(ctx, len + 1);
result = xmpp_alloc(ctx, c-jid+1);
if (result != NULL) { if (result != NULL) {
memcpy(result, jid, c-jid); memcpy(result, jid, len);
result[c-jid] = '\0'; result[len] = '\0';
} }
return result; return result;
@@ -121,7 +119,8 @@ char *xmpp_jid_node(xmpp_ctx_t *ctx, const char *jid)
char *xmpp_jid_domain(xmpp_ctx_t *ctx, const char *jid) char *xmpp_jid_domain(xmpp_ctx_t *ctx, const char *jid)
{ {
char *result = NULL; char *result = NULL;
const char *c,*s; const char *c;
size_t dlen;
c = strchr(jid, '@'); c = strchr(jid, '@');
if (c == NULL) { if (c == NULL) {
@@ -131,15 +130,11 @@ char *xmpp_jid_domain(xmpp_ctx_t *ctx, const char *jid)
/* advance past the separator */ /* advance past the separator */
c++; c++;
} }
s = strchr(c, '/'); dlen = strcspn(c, "/"); /* do not include resource */
if (s == NULL) { result = xmpp_alloc(ctx, dlen + 1);
/* no resource */
s = c + strlen(c);
}
result = xmpp_alloc(ctx, (s-c) + 1);
if (result != NULL) { if (result != NULL) {
memcpy(result, c, (s-c)); memcpy(result, c, dlen);
result[s-c] = '\0'; result[dlen] = '\0';
} }
return result; return result;
@@ -155,20 +150,8 @@ char *xmpp_jid_domain(xmpp_ctx_t *ctx, const char *jid)
*/ */
char *xmpp_jid_resource(xmpp_ctx_t *ctx, const char *jid) char *xmpp_jid_resource(xmpp_ctx_t *ctx, const char *jid)
{ {
char *result = NULL;
const char *c; const char *c;
size_t len;
c = strchr(jid, '/'); c = strchr(jid, '/');
if (c != NULL) { return c != NULL ? xmpp_strdup(ctx, c + 1) : NULL;
c++;
len = strlen(c);
result = xmpp_alloc(ctx, len + 1);
if (result != NULL) {
memcpy(result, c, len);
result[len] = '\0';
}
}
return result;
} }