編輯:關於 Android 開發
在 MySQL 的原始碼中,經常可見關於動態字符串的處理,例如 DYNAMIC_STRING。為了記錄動態字符串的實際長度、緩衝區的最大容量,並在需要時及時配置新記憶體與調整長度,MySQL 使用 DYNAMIC_STRING 來保存這些資訊:
typedef struct st_dynamic_string
{
char *str;
size_t length, max_length, alloc_increment;
} DYNAMIC_STRING;
在這個結構體中,str 儲存實際字符串的首地址,length 記錄實際長度,max_length 記錄緩衝區最多可存放多少字元,alloc_increment 則表示每次擴容時配置多少記憶體。
my_bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str, size_t init_alloc, size_t alloc_increment)
{
size_t length;
DBUG_ENTER("init_dynamic_string");
if (!alloc_increment)
alloc_increment = 128;
length = 1;
if (init_str && (length = strlen(init_str) + 1) < init_alloc)
init_alloc = ((length + alloc_increment - 1) / alloc_increment) * alloc_increment;
if (!init_alloc)
init_alloc = alloc_increment;
if (!(str->str = (char*) my_malloc(init_alloc, MYF(MY_WME))))
DBUG_RETURN(TRUE);
str->length = length - 1;
if (init_str)
memcpy(str->str, init_str, length);
str->max_length = init_alloc;
str->alloc_increment = alloc_increment;
DBUG_RETURN(FALSE);
}
可見初始化時,初始配置的緩衝區大小 init_alloc 會依據初始字符串調整。配置完成後,再依緩衝區大小、實際長度與 alloc_increment 完成初始化,如此一來,後續追加內容時就能依這些資訊判斷是否需要擴容。
my_bool dynstr_append_mem(DYNAMIC_STRING *str, const char *append, size_t length)
{
char *new_ptr;
if (str->length + length >= str->max_length) //如果新增字符串後,總長度超過緩衝區大小
{
//計算需要幾個 alloc_increment 單元
size_t new_length = (str->length + length + str->alloc_increment) / str->alloc_increment;
new_length *= str->alloc_increment;
if (!(new_ptr = (char*) my_realloc(str->str, new_length, MYF(MY_WME))))
return TRUE;
str->str = new_ptr;
str->max_length = new_length;
}
memcpy(str->str + str->length, append, length);
str->length += length;
str->str[str->length] = 0; /* Safety for C programs */
return FALSE;
}
由於結構體記錄了字符串的完整資訊,追加內容時只需依先前儲存的資訊動態 realloc 即可,擴容處理非常方便。
字符串截斷:
my_bool dynstr_trunc(DYNAMIC_STRING *str, size_t n)
{
str->length -= n;
str->str[str->length] = '\0';
return FALSE;
}
尋找第一個出現某字元的位置,若無則返回指向結尾 '\0' 的地址:
char *strcend(register const char *s, register pchar c)
{
for (;;)
{
if (*s == (char) c) return (char*) s;
if (!*s++) return (char*) s-1;
}
}
字符串內容擴容:
my_bool dynstr_realloc(DYNAMIC_STRING *str, size_t additional_size)
{
DBUG_ENTER("dynstr_realloc");
if (!additional_size) DBUG_RETURN(FALSE);
if (str->length + additional_size > str->max_length)
{
str->max_length = ((str->length + additional_size + str->alloc_increment - 1) / str->alloc_increment) * str->alloc_increment;
if (!(str->str = (char*) my_realloc(str->str, str->max_length, MYF(MY_WME))))
DBUG_RETURN(TRUE);
}
DBUG_RETURN(FALSE);
}
此外,還有為字符串加上引號並轉義其中單引號的功能,主要用於執行系統命令(system(cmd))。例如 ls -al 會變成 'ls -al',而含單引號的字串則會被適當轉義:
my_bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...)
{
const char *quote_str = "'";
const uint quote_len = 1;
my_bool ret = TRUE;
va_list dirty_text;
ret &= dynstr_append_mem(str, quote_str, quote_len); /* Leading quote */
va_start(dirty_text, append);
while (append != NullS)
{
const char *cur_pos = append;
const char *next_pos = cur_pos;
while (*(next_pos = strcend(cur_pos, quote_str[0])) != '\0')
{
ret &= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos));
ret &= dynstr_append_mem(str, "\\", 1);
ret &= dynstr_append_mem(str, quote_str, quote_len);
cur_pos = next_pos + 1;
}
ret &= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos));
append = va_arg(dirty_text, char *);
}
va_end(dirty_text);
ret &= dynstr_append_mem(str, quote_str, quote_len); /* Trailing quote */
return ret;
}
透過定義動態字符串的結構體,每次追加內容時都能依當前長度動態擴容,且擴容後結構體都記錄著最新的實際資訊(當前長度、緩衝區容量、擴容單元大小),讓動態字符串的處理變得既直覺又高效。
Android ListView 入門教學與效能優化要點
ListView 是 Android 早期最基礎的列表控制元件,理解它的運作方式與優化技巧,至今仍具參考價值。本文從最簡單的例子出發,示範如何實作並優化 ListView
硅谷社交 12:群列表頁面的完整實作(含適配器與伺服器同步)
硅谷社交 12:群列表頁面的完整實作(含適配器與伺服器同步) 群列表是使用者進入群聊的入口。它需要解決三件事:把資料渲染成列表、從環信伺服器拉取最新群組、以及在點擊後帶
Android 面試題及答案(二):核心知識點匯總
Android 面試常見問題與解答彙編 本文整理了 Android 面試中經常出現的核心知識點與參考答案,涵蓋 Activity、Fragment、序列化、多線程、圖片加
【原創】來自遠方的「僵屍之手」:遠端遙控型 Android 惡意程式剖析
【原創】來自遠方的「僵屍之手」:遠端遙控型 Android 惡意程式剖析 來自遠方的「僵屍之手」 安天 AVL 移動安全實驗室與獵豹移動安全實驗室近期共同截獲一款名為「僵