Merge pull request #17235 from taosdata/feat/TD-17777-V30
feat(shell): Supported the word completed by press Tab key for 3.0
This commit is contained in:
commit
1c106bfd1d
|
@ -33,38 +33,35 @@
|
||||||
typedef struct STireNode {
|
typedef struct STireNode {
|
||||||
struct STireNode** d;
|
struct STireNode** d;
|
||||||
bool end; // record end flag
|
bool end; // record end flag
|
||||||
}STireNode;
|
} STireNode;
|
||||||
|
|
||||||
typedef struct StrName {
|
typedef struct StrName {
|
||||||
char * name;
|
char* name;
|
||||||
struct StrName * next;
|
struct StrName* next;
|
||||||
}StrName;
|
} StrName;
|
||||||
|
|
||||||
|
|
||||||
typedef struct STire {
|
typedef struct STire {
|
||||||
char type; // see define TIRE_
|
char type; // see define TIRE_
|
||||||
STireNode root;
|
STireNode root;
|
||||||
|
|
||||||
StrName * head;
|
StrName* head;
|
||||||
StrName * tail;
|
StrName* tail;
|
||||||
|
|
||||||
int count; // all count
|
int count; // all count
|
||||||
int ref;
|
int ref;
|
||||||
}STire;
|
} STire;
|
||||||
|
|
||||||
typedef struct SMatchNode {
|
typedef struct SMatchNode {
|
||||||
char* word;
|
char* word;
|
||||||
struct SMatchNode* next;
|
struct SMatchNode* next;
|
||||||
}SMatchNode;
|
} SMatchNode;
|
||||||
|
|
||||||
|
|
||||||
typedef struct SMatch {
|
typedef struct SMatch {
|
||||||
SMatchNode* head;
|
SMatchNode* head;
|
||||||
SMatchNode* tail; // append node to tail
|
SMatchNode* tail; // append node to tail
|
||||||
int count;
|
int count;
|
||||||
char pre[MAX_WORD_LEN];
|
char pre[MAX_WORD_LEN];
|
||||||
}SMatch;
|
} SMatch;
|
||||||
|
|
||||||
|
|
||||||
// ----------- interface -------------
|
// ----------- interface -------------
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -26,17 +26,16 @@ STire* createTire(char type) {
|
||||||
memset(tire, 0, sizeof(STire));
|
memset(tire, 0, sizeof(STire));
|
||||||
tire->ref = 1; // init is 1
|
tire->ref = 1; // init is 1
|
||||||
tire->type = type;
|
tire->type = type;
|
||||||
tire->root.d = (STireNode **)taosMemoryCalloc(CHAR_CNT, sizeof(STireNode *));
|
tire->root.d = (STireNode**)taosMemoryCalloc(CHAR_CNT, sizeof(STireNode*));
|
||||||
return tire;
|
return tire;
|
||||||
}
|
}
|
||||||
|
|
||||||
// free tire node
|
// free tire node
|
||||||
void freeTireNode(STireNode* node) {
|
void freeTireNode(STireNode* node) {
|
||||||
if (node == NULL)
|
if (node == NULL) return;
|
||||||
return ;
|
|
||||||
|
|
||||||
// nest free sub node on array d
|
// nest free sub node on array d
|
||||||
if(node->d) {
|
if (node->d) {
|
||||||
for (int i = 0; i < CHAR_CNT; i++) {
|
for (int i = 0; i < CHAR_CNT; i++) {
|
||||||
freeTireNode(node->d[i]);
|
freeTireNode(node->d[i]);
|
||||||
}
|
}
|
||||||
|
@ -56,9 +55,9 @@ void freeTire(STire* tire) {
|
||||||
taosMemoryFree(tire->root.d);
|
taosMemoryFree(tire->root.d);
|
||||||
|
|
||||||
// free from list
|
// free from list
|
||||||
StrName * item = tire->head;
|
StrName* item = tire->head;
|
||||||
while (item) {
|
while (item) {
|
||||||
StrName * next = item->next;
|
StrName* next = item->next;
|
||||||
// free string
|
// free string
|
||||||
taosMemoryFree(item->name);
|
taosMemoryFree(item->name);
|
||||||
// free node
|
// free node
|
||||||
|
@ -75,14 +74,14 @@ void freeTire(STire* tire) {
|
||||||
|
|
||||||
// insert a new word to list
|
// insert a new word to list
|
||||||
bool insertToList(STire* tire, char* word) {
|
bool insertToList(STire* tire, char* word) {
|
||||||
StrName * p = (StrName *)taosMemoryMalloc(sizeof(StrName));
|
StrName* p = (StrName*)taosMemoryMalloc(sizeof(StrName));
|
||||||
p->name = strdup(word);
|
p->name = strdup(word);
|
||||||
p->next = NULL;
|
p->next = NULL;
|
||||||
|
|
||||||
if(tire->head == NULL) {
|
if (tire->head == NULL) {
|
||||||
tire->head = p;
|
tire->head = p;
|
||||||
tire->tail = p;
|
tire->tail = p;
|
||||||
}else {
|
} else {
|
||||||
tire->tail->next = p;
|
tire->tail->next = p;
|
||||||
tire->tail = p;
|
tire->tail = p;
|
||||||
}
|
}
|
||||||
|
@ -93,7 +92,7 @@ bool insertToList(STire* tire, char* word) {
|
||||||
// insert a new word to tree
|
// insert a new word to tree
|
||||||
bool insertToTree(STire* tire, char* word, int len) {
|
bool insertToTree(STire* tire, char* word, int len) {
|
||||||
int m = 0;
|
int m = 0;
|
||||||
STireNode ** nodes = tire->root.d;
|
STireNode** nodes = tire->root.d;
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
m = word[i] - FIRST_ASCII;
|
m = word[i] - FIRST_ASCII;
|
||||||
if (m < 0 || m > CHAR_CNT) {
|
if (m < 0 || m > CHAR_CNT) {
|
||||||
|
@ -102,7 +101,7 @@ bool insertToTree(STire* tire, char* word, int len) {
|
||||||
|
|
||||||
if (nodes[m] == NULL) {
|
if (nodes[m] == NULL) {
|
||||||
// no pointer
|
// no pointer
|
||||||
STireNode* p = (STireNode* )taosMemoryMalloc(sizeof(STireNode));
|
STireNode* p = (STireNode*)taosMemoryMalloc(sizeof(STireNode));
|
||||||
memset(p, 0, sizeof(STireNode));
|
memset(p, 0, sizeof(STireNode));
|
||||||
nodes[m] = p;
|
nodes[m] = p;
|
||||||
if (i == len - 1) {
|
if (i == len - 1) {
|
||||||
|
@ -114,7 +113,7 @@ bool insertToTree(STire* tire, char* word, int len) {
|
||||||
|
|
||||||
if (nodes[m]->d == NULL) {
|
if (nodes[m]->d == NULL) {
|
||||||
// malloc d
|
// malloc d
|
||||||
nodes[m]->d = (STireNode **)taosMemoryCalloc(CHAR_CNT, sizeof(STireNode *));
|
nodes[m]->d = (STireNode**)taosMemoryCalloc(CHAR_CNT, sizeof(STireNode*));
|
||||||
}
|
}
|
||||||
|
|
||||||
// move to next node
|
// move to next node
|
||||||
|
@ -146,7 +145,7 @@ bool insertWord(STire* tire, char* word) {
|
||||||
|
|
||||||
// delete one word from list
|
// delete one word from list
|
||||||
bool deleteFromList(STire* tire, char* word) {
|
bool deleteFromList(STire* tire, char* word) {
|
||||||
StrName * item = tire->head;
|
StrName* item = tire->head;
|
||||||
while (item) {
|
while (item) {
|
||||||
if (strcmp(item->name, word) == 0) {
|
if (strcmp(item->name, word) == 0) {
|
||||||
// found, reset empty to delete
|
// found, reset empty to delete
|
||||||
|
@ -176,7 +175,7 @@ bool deleteFromTree(STire* tire, char* word, int len) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
// not null
|
// not null
|
||||||
if(i == len - 1) {
|
if (i == len - 1) {
|
||||||
// this is last, only set end false , not free node
|
// this is last, only set end false , not free node
|
||||||
nodes[m]->end = false;
|
nodes[m]->end = false;
|
||||||
del = true;
|
del = true;
|
||||||
|
@ -184,8 +183,7 @@ bool deleteFromTree(STire* tire, char* word, int len) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(nodes[m]->d == NULL)
|
if (nodes[m]->d == NULL) break;
|
||||||
break;
|
|
||||||
// move to next node
|
// move to next node
|
||||||
nodes = nodes[m]->d;
|
nodes = nodes[m]->d;
|
||||||
}
|
}
|
||||||
|
@ -216,9 +214,9 @@ bool deleteWord(STire* tire, char* word) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void addWordToMatch(SMatch* match, char* word){
|
void addWordToMatch(SMatch* match, char* word) {
|
||||||
// malloc new
|
// malloc new
|
||||||
SMatchNode* node = (SMatchNode* )taosMemoryMalloc(sizeof(SMatchNode));
|
SMatchNode* node = (SMatchNode*)taosMemoryMalloc(sizeof(SMatchNode));
|
||||||
memset(node, 0, sizeof(SMatchNode));
|
memset(node, 0, sizeof(SMatchNode));
|
||||||
node->word = strdup(word);
|
node->word = strdup(word);
|
||||||
|
|
||||||
|
@ -234,7 +232,7 @@ void addWordToMatch(SMatch* match, char* word){
|
||||||
|
|
||||||
// enum all words from node
|
// enum all words from node
|
||||||
void enumAllWords(STireNode** nodes, char* prefix, SMatch* match) {
|
void enumAllWords(STireNode** nodes, char* prefix, SMatch* match) {
|
||||||
STireNode * c;
|
STireNode* c;
|
||||||
char word[MAX_WORD_LEN];
|
char word[MAX_WORD_LEN];
|
||||||
int len = strlen(prefix);
|
int len = strlen(prefix);
|
||||||
for (int i = 0; i < CHAR_CNT; i++) {
|
for (int i = 0; i < CHAR_CNT; i++) {
|
||||||
|
@ -255,18 +253,17 @@ void enumAllWords(STireNode** nodes, char* prefix, SMatch* match) {
|
||||||
addWordToMatch(match, word);
|
addWordToMatch(match, word);
|
||||||
}
|
}
|
||||||
// nested call next layer
|
// nested call next layer
|
||||||
if (c->d)
|
if (c->d) enumAllWords(c->d, word, match);
|
||||||
enumAllWords(c->d, word, match);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// match prefix from list
|
// match prefix from list
|
||||||
void matchPrefixFromList(STire* tire, char* prefix, SMatch* match) {
|
void matchPrefixFromList(STire* tire, char* prefix, SMatch* match) {
|
||||||
StrName * item = tire->head;
|
StrName* item = tire->head;
|
||||||
int len = strlen(prefix);
|
int len = strlen(prefix);
|
||||||
while (item) {
|
while (item) {
|
||||||
if ( strncmp(item->name, prefix, len) == 0) {
|
if (strncmp(item->name, prefix, len) == 0) {
|
||||||
// prefix matched
|
// prefix matched
|
||||||
addWordToMatch(match, item->name);
|
addWordToMatch(match, item->name);
|
||||||
}
|
}
|
||||||
|
@ -304,29 +301,27 @@ void matchPrefixFromTree(STire* tire, char* prefix, SMatch* match) {
|
||||||
if (i == len - 1) {
|
if (i == len - 1) {
|
||||||
// malloc match if not pass by param match
|
// malloc match if not pass by param match
|
||||||
if (root == NULL) {
|
if (root == NULL) {
|
||||||
root = (SMatch* )taosMemoryMalloc(sizeof(SMatch));
|
root = (SMatch*)taosMemoryMalloc(sizeof(SMatch));
|
||||||
memset(root, 0, sizeof(SMatch));
|
memset(root, 0, sizeof(SMatch));
|
||||||
strcpy(root->pre, prefix);
|
strcpy(root->pre, prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
// prefix is match to end char
|
// prefix is match to end char
|
||||||
if (c->d)
|
if (c->d) enumAllWords(c->d, prefix, root);
|
||||||
enumAllWords(c->d, prefix, root);
|
|
||||||
} else {
|
} else {
|
||||||
// move to next node continue match
|
// move to next node continue match
|
||||||
if(c->d == NULL)
|
if (c->d == NULL) break;
|
||||||
break;
|
|
||||||
nodes = c->d;
|
nodes = c->d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// return
|
// return
|
||||||
return ;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SMatch* matchPrefix(STire* tire, char* prefix, SMatch* match) {
|
SMatch* matchPrefix(STire* tire, char* prefix, SMatch* match) {
|
||||||
if(match == NULL) {
|
if (match == NULL) {
|
||||||
match = (SMatch* )taosMemoryMalloc(sizeof(SMatch));
|
match = (SMatch*)taosMemoryMalloc(sizeof(SMatch));
|
||||||
memset(match, 0, sizeof(SMatch));
|
memset(match, 0, sizeof(SMatch));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,10 +343,9 @@ SMatch* matchPrefix(STire* tire, char* prefix, SMatch* match) {
|
||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// get all items from tires tree
|
// get all items from tires tree
|
||||||
void enumFromList(STire* tire, SMatch* match) {
|
void enumFromList(STire* tire, SMatch* match) {
|
||||||
StrName * item = tire->head;
|
StrName* item = tire->head;
|
||||||
while (item) {
|
while (item) {
|
||||||
if (item->name[0] != 0) {
|
if (item->name[0] != 0) {
|
||||||
// not delete
|
// not delete
|
||||||
|
@ -365,7 +359,7 @@ void enumFromList(STire* tire, SMatch* match) {
|
||||||
|
|
||||||
// get all items from tires tree
|
// get all items from tires tree
|
||||||
void enumFromTree(STire* tire, SMatch* match) {
|
void enumFromTree(STire* tire, SMatch* match) {
|
||||||
char pre[2] ={0, 0};
|
char pre[2] = {0, 0};
|
||||||
STireNode* c;
|
STireNode* c;
|
||||||
|
|
||||||
// enum first layer
|
// enum first layer
|
||||||
|
@ -380,7 +374,7 @@ void enumFromTree(STire* tire, SMatch* match) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// this branch have data
|
// this branch have data
|
||||||
if(c->end)
|
if (c->end)
|
||||||
addWordToMatch(match, pre);
|
addWordToMatch(match, pre);
|
||||||
else
|
else
|
||||||
matchPrefix(tire, pre, match);
|
matchPrefix(tire, pre, match);
|
||||||
|
@ -389,7 +383,7 @@ void enumFromTree(STire* tire, SMatch* match) {
|
||||||
|
|
||||||
// get all items from tires tree
|
// get all items from tires tree
|
||||||
SMatch* enumAll(STire* tire) {
|
SMatch* enumAll(STire* tire) {
|
||||||
SMatch* match = (SMatch* )taosMemoryMalloc(sizeof(SMatch));
|
SMatch* match = (SMatch*)taosMemoryMalloc(sizeof(SMatch));
|
||||||
memset(match, 0, sizeof(SMatch));
|
memset(match, 0, sizeof(SMatch));
|
||||||
|
|
||||||
switch (tire->type) {
|
switch (tire->type) {
|
||||||
|
@ -410,16 +404,13 @@ SMatch* enumAll(STire* tire) {
|
||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// free match result
|
// free match result
|
||||||
void freeMatchNode(SMatchNode* node) {
|
void freeMatchNode(SMatchNode* node) {
|
||||||
// first free next
|
// first free next
|
||||||
if (node->next)
|
if (node->next) freeMatchNode(node->next);
|
||||||
freeMatchNode(node->next);
|
|
||||||
|
|
||||||
// second free self
|
// second free self
|
||||||
if (node->word)
|
if (node->word) taosMemoryFree(node->word);
|
||||||
taosMemoryFree(node->word);
|
|
||||||
taosMemoryFree(node);
|
taosMemoryFree(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue