나는 명령을 저장하기 위해 C로 데이터 구조를 작성하고있다. 여기에 내가에 만족 해요 무엇을 아래로 깎았 원인이 완벽하게 잘 작동명시 적 문자열 복사없이 char 문자열과 wchar_t 문자열 사이에서 함수 논리를 다시 사용 하시겠습니까?
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include "dbg.h"
#include "commandtree.h"
struct BranchList
{
CommandTree *tree;
BranchList *next;
};
struct CommandTree
{
wchar_t id; // wchar support actually has no memory cost due to the
bool term; // padding that would otherwise exist, and may in fact be
BranchList *list; // marginally faster to access due to its alignable size.
};
static inline BranchList *BranchList_create(void)
{
return calloc(1, sizeof(BranchList));
}
inline CommandTree *CommandTree_create(void)
{
return calloc(1, sizeof(CommandTree));
}
int CommandTree_putnw(CommandTree *t, const wchar_t *s, size_t n)
{
for(BranchList **p = &t->list;;)
{
if(!*p)
{
*p = BranchList_create();
if(errno == ENOMEM) return 1;
(*p)->tree = CommandTree_create();
if(errno == ENOMEM) return 1;
(*p)->tree->id = *s;
}
else if(*s != (*p)->tree->id)
{
p = &(*p)->next;
continue;
}
if(n == 1)
{
(*p)->tree->term = 1;
return 0;
}
p = &(*p)->tree->list;
s++;
n--;
}
}
int CommandTree_putn(CommandTree *t, const char *s, size_t n)
{
wchar_t *passto = malloc(n * sizeof(wchar_t));
mbstowcs(passto, s, n);
int ret = CommandTree_putnw(t, passto, n);
free(passto);
return ret;
}
,하지만 내 나무 wchar_t
를 지원하는지 나는 사실을 처리하고있어 방법에 오히려 만족 해요. CommandTree
의 패딩을 사용하면 7 바이트보다 작은 데이터 유형을 만들 수 있다는 것을 알았을 때이 코드를 추가하기로 결정 했으므로 코드를 복제하지 않으므로 의 논리를 다시 사용하십시오.
그러나 char
과 wchar_t
의 크기 차이로 인해 필자는 배열을 전달할 수 없습니다. 을 CommandTree_putnw
으로 전달해야합니다. CommandTree_putn
이 가장 많이 사용되며 저장된 문자열의 메모리 사용량 (sizeof (char)
~ sizeof (char) + sizeof (wchar_t)
)이 5 배로 길어 졌다고 가정 할 때이 옵션은 차선입니다. 이러한 문자열이 오래 걸리는 명령으로 인스턴스화 될 경우 스택 할 수 있습니다.
void *
에 하나
const char *
또는로 전달 된 문자열을 던졌다있는 값에 따라
size_t
을 전달받을를 궁금 해서요
const wchar_t *
하지만 C가 정적으로 입력되면, 나는 논리를 "논리의 단일 인스턴스"로 옮길 생각을 망칠 수있는 각각의 유형으로 캐스팅 된 로직을
s
으로 복제해야합니다.
그래서 궁극적으로 문제는, 내가 한 번만 프로그램 로직을 제공 할 수 const char *
를 처리하는 기능에 임시 wchar_t *
을 만들지 않고, 각각 래퍼 const char *
및 const wchar_t *
을 통과?
독서를 쉽게하기 위해 거대한 단락을 별도의 문장으로 자르십시오! –