glib字符串

學過面向對象語言的同學一定都知道String類,一定知道這個類對字符串的操作是多麼的方便,但是c語言中是沒有這個類,甚至沒有類的概念,但是glib幫我們做的這個“類” ?GString


除了使用gchar *進行字符串處理以外,Glib還定義了一種新的數據類型: GString。
它類似于標準C的字符串類型,但是GString能夠自動增長。它的字符串數據是以NULL結尾的。
這些特性可以防止程序中的緩沖溢出。這是一種非常重要的特性。



GString是一種動態字符串數據結構,提供了豐富靈活方便的API,無需手工分配內存,無需擔心緩沖區溢出。

先說一下GString的結構體定義

GString

typedef struct {gchar  *str;gsize len;    gsize allocated_len;
} GString;

這里就不用多解釋了,str是字符串的指針,len是字符串長度,allocated_len是前面說的會多申請一段內存,申請的大小是由一定的算法,并不是保留一個固定值,下面的例子程序中有相關的效果,可以留意一下。


然后是glib提供的功能函數:

Synopsis

#include <glib.h>GString;
GString*            g_string_new                        (const gchar *init);
GString*            g_string_new_len                    (const gchar *init,gssize len);
GString*            g_string_sized_new                  (gsize dfl_size);
GString*            g_string_assign                     (GString *string,const gchar *rval);
#define             g_string_sprintf
#define             g_string_sprintfa
void                g_string_vprintf                    (GString *string,const gchar *format,va_list args);
void                g_string_append_vprintf             (GString *string,const gchar *format,va_list args);
void                g_string_printf                     (GString *string,const gchar *format,...);
void                g_string_append_printf              (GString *string,const gchar *format,...);
GString*            g_string_append                     (GString *string,const gchar *val);
GString*            g_string_append_c                   (GString *string,gchar c);
GString*            g_string_append_unichar             (GString *string,gunichar wc);
GString*            g_string_append_len                 (GString *string,const gchar *val,gssize len);
GString*            g_string_append_uri_escaped         (GString *string,const char *unescaped,const char *reserved_chars_allowed,gboolean allow_utf8);
GString*            g_string_prepend                    (GString *string,const gchar *val);
GString*            g_string_prepend_c                  (GString *string,gchar c);
GString*            g_string_prepend_unichar            (GString *string,gunichar wc);
GString*            g_string_prepend_len                (GString *string,const gchar *val,gssize len);
GString*            g_string_insert                     (GString *string,gssize pos,const gchar *val);
GString*            g_string_insert_c                   (GString *string,gssize pos,gchar c);
GString*            g_string_insert_unichar             (GString *string,gssize pos,gunichar wc);
GString*            g_string_insert_len                 (GString *string,gssize pos,const gchar *val,gssize len);
GString*            g_string_overwrite                  (GString *string,gsize pos,const gchar *val);
GString*            g_string_overwrite_len              (GString *string,gsize pos,const gchar *val,gssize len);
GString*            g_string_erase                      (GString *string,gssize pos,gssize len);
GString*            g_string_truncate                   (GString *string,gsize len);
GString*            g_string_set_size                   (GString *string,gsize len);
gchar*              g_string_free                       (GString *string,gboolean free_segment);GString*            g_string_up                         (GString *string);
GString*            g_string_down                       (GString *string);guint               g_string_hash                       (const GString *str);
gboolean            g_string_equal                      (const GString *v,const GString *v2);

用下面的函數創建新的GString變量: GString *g_string_new( gchar *init ); 這個函數創建一個GString,將字符串值init復制到GString中,返回一個指向它的指針。 如果init參數是NULL,創建一個空GString。?
void g_string_free( GString *string, gint free_segment ); 這個函數釋放string所占據的內存。
free_segment參數是一個布爾類型變量。如果 free_segment參數是TRUE,它還釋放其中的字符數據。
?GString *g_string_assign( GString *lval, const gchar *rval ); 這 個函數將字符從rval復制到lval,銷毀lval的原有內容。注意,如有必要, lval會被加長以容納字符串的內容。這一點和標準的字符串復制函數strcpy( )相同。下面的函數的意義都是顯而易見的。
其中以_ c結尾的函數接受一個字符,而不是字符串。?
截取string字符串,生成一個長度為len的子串: GString *g_string_truncate( GString *string, gint len );?
將字符串val追加在string后面,返回一個新字符串: GString *g_string_append( GString *string, gchar *val );?
將字符c追加到string后面,返回一個新的字符串: GString *g_string_append_c( GString *string, gchar c );?
將字符串val插入到string前面,生成一個新字符串: GString *g_string_prepend( GString *string, gchar *val );?
將字符c插入到string前面,生成一個新字符串: GString *g_string_prepend_c( GString *string, gchar c );?
將一個格式化的字符串寫到string中,類似于標準的sprint f函數: void g_string_sprintf( GString *string, gchar *fmt, . . . ) ;?
將一個格式化字符串追加到string后面,與上一個函數略有不同: void g_string_sprintfa ( GString *string, gchar *fmt, ... );


下面用一個例子程序說明幾個常用函數的用法

[cpp]?view plain?copy
  1. #include?<glib.h>??
  2. ??
  3. int?main(int?argc,char?**argv)?{??
  4. ??
  5. //????GString*?g_string_new(const?gchar?*init);???根據所給的字符串建立適當長度的GString,并自動創建適當長度的allocated_len,創建的時候將字符串init復制到GString中。??
  6. ????GString?*string?=?g_string_new("Justin");??
  7. ????g_print("g_string_new(\"Justin\");?len?=?%d,?allocated_len?=?%d\n",???
  8. ????????????string->len,?string->allocated_len);??
  9. ??????
  10. //????若不使用GString時,可以使用g_string_free()釋放,第二個參數如果為TRUE,會連同字符串也是放掉。??
  11. ????g_string_free(string,?TRUE);??????????????
  12. ??
  13. //????GString*?g_string_new_len(const?gchar?*init,?gssize?len);?????指定len來建立GString,因為是自行制定的長度,所以len必須超過init的長度。??
  14. ????string?=?g_string_new_len("Justin",?32);??
  15. ????g_print("g_string_new_len(\"Justin\",?32);?len?=?%d,?allocated_len?=?%d\n",???
  16. ????????????string->len,?string->allocated_len);??????
  17. ????g_string_free(string,?TRUE);??????????????
  18. ??
  19. //????GString*?g_string_sized_new(gsize?dfl_size);??????指定allocated_len建立GString,函數會根據需要的長度值自動分配長度,也就是不一定會分配設置的長度,但一定會比設定的長度大。??
  20. ????string?=?g_string_sized_new(32);??
  21. ????g_printf("g_string_sized_new(32);?len?=?%d,?allocated_len?=?%d\n",???
  22. ????????????string->len,?string->allocated_len);???
  23. ????g_string_free(string,?TRUE);?????
  24. ??
  25. ??
  26. ????GString?*t=g_string_new("Hello?Laomeng.");??
  27. ????GString?*s=g_string_new("Hello?Laoli.");??
  28. ??????
  29. //????g_string_equal()????判斷兩個GString的字符串內容是否相同??
  30. ????if(g_string_equal(s,t))???
  31. ????{??
  32. ????????g_print("g_string_equal(s,t);?%s??==?%s\n",s->str,t->str);??
  33. ????}???
  34. ????else???
  35. ????{??
  36. ????????g_print("g_string_equal(s,t);?%s?!=?%s\n",s->str,t->str);??
  37. ????}??
  38. ????g_string_free(s,?TRUE);???
  39. ????g_string_free(t,?TRUE);???
  40. ??
  41. ????string?=?g_string_new("first?str!");??
  42. //?????GString*?g_string_append?(GString?*string,const?gchar?*val);?????在字符串string后面追加字符串val??
  43. ????g_string_append(string,?"second?str!");??
  44. ????g_print("g_string_append(string,?\"second?str!\");?%s\n",?string->str);??
  45. ??
  46. //????GString*?g_string_truncate(GString?*string,gsize?len);?截斷字符串,保留前len個字符??
  47. ????g_string_truncate(string,5);??
  48. ????g_print("g_string_truncate(string,5);??string:??%s,?len?=?%d,?allocated_len?=?%d\n",string->str,?string->len?,?string->allocated_len);??
  49. ??
  50. //?????GString*?g_string_prepend?(GString?*string,const?gchar?*val);?????在字符串string前面追加字符串val??
  51. ????g_string_prepend(string,?"prepend?str!");??
  52. ????g_print("g_string_prepend(string,?\"prepend?str!\");?string:%s\n",?string->str);??
  53. ??
  54. //?????GString*?g_string_insert?(GString?*string,gssize?pos,const?gchar?*val);?????將字符串插入到pos處??
  55. ????g_string_insert(string,?sizeof("prepend?str!")-1?,?"insert?str!");??
  56. ????g_print("g_string_insert(string,?sizeof(\"prepend?str!\")?,?\"insert?str!\");?%s\n",?string->str);??
  57. ??
  58. //????void?g_string_printf(GString?*string,?const?gchar?*format,?...);??格式化一個字符串,和sprintf用法一樣,只是賦值給一個GString,string里面以前的數據被銷毀了??
  59. ????g_string_printf(string,"%d?+?%d?=?%d",?100,?200,?100+200);??
  60. ????g_print("g_string_printf();?GString:?%s,?len?=?%d,?allocated_len?=?%d?\n",string->str,?string->len?,?string->allocated_len);??
  61. ??
  62. //????void?g_string_append_printf(GString?*string,?const?gchar?*format,?...);??格式化一個字符串,和g_string_printf很相似,只是不銷毀string里面的數據,而是在后面追加??
  63. ????g_string_append_printf(string,"\t?%d?*?%d?=?%d",?100,200,100*200);??
  64. ????g_print("g_string_append_printf();?GString:?%s,?len?=?%d,?allocated_len?=?%d?\n",string->str,?string->len?,?string->allocated_len);??
  65. ??
  66. ????g_string_free(string,TRUE);??
  67. ??
  68. ????string?=?g_string_new("abcdefgABCDEFG");??
  69. ??
  70. //????g_string_ascii_up()或g_utf8_strup()?轉換GString中的字符串為小寫??
  71. ????g_string_ascii_up(string);??
  72. ????g_printf("Upper:?%s\n",?string->str);??
  73. //????g_string_ascii_down()或g_utf8_strdown()轉換GString中的字符串為大寫??
  74. ????g_string_ascii_down(string);??
  75. ????g_printf("Down:?%s\n",?string->str);??
  76. ??
  77. ????g_string_free(string,TRUE);??
  78. ??
  79. ????return?0;??
  80. ??
  81. }??

運行結果:

[plain]?view plain?copy
  1. linux@ubuntu:~/16021/glibdemo$?gcc?-o?gstring?gstring.c?-lglib-2.0??
  2. linux@ubuntu:~/16021/glibdemo$?./gstring???
  3. g_string_new("Justin");?len?=?6,?allocated_len?=?16??
  4. g_string_new_len("Justin",?32);?len?=?32,?allocated_len?=?64??
  5. g_string_sized_new(32);?len?=?0,?allocated_len?=?64??
  6. g_string_equal(s,t);?Hello?Laoli.?!=?Hello?Laomeng.??
  7. g_string_append(string,?"second?str!");?first?str!second?str!??
  8. g_string_truncate(string,5);??string:??first,?len?=?5,?allocated_len?=?32??
  9. g_string_prepend(string,?"prepend?str!");?string:prepend?str!first??
  10. g_string_insert(string,?sizeof("prepend?str!")?,?"insert?str!");?prepend?str!insert?str!first??
  11. g_string_printf();?GString:?100?+?200?=?300,?len?=?15,?allocated_len?=?32???
  12. g_string_append_printf();?GString:?100?+?200?=?300???100?*?200?=?20000,?len?=?34,?allocated_len?=?64???
  13. Upper:?ABCDEFGABCDEFG??
  14. Down:?abcdefgabcdefg??
  15. linux@ubuntu:~/16021/glibdemo$ ??

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385058.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385058.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385058.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

KMP 串的模式匹配 (25 分)

給定兩個由英文字母組成的字符串 String 和 Pattern&#xff0c;要求找到 Pattern 在 String 中第一次出現的位置&#xff0c;并將此位置后的 String 的子串輸出。如果找不到&#xff0c;則輸出“Not Found”。 本題旨在測試各種不同的匹配算法在各種數據情況下的表現。各組測試…

命令行工具tshark使用小記

1、目的 寫這篇博客的目的主要是為了方便查閱&#xff0c;使用wireshark可以分析數據包&#xff0c;可以通過編輯過濾表達式來達到對數據的分析&#xff1b;但我的需求是&#xff0c;怎么樣把Data部分導出來&#xff0c;因為后續的工作主要針對數據包的Data部分&#xff0c;主要…

winshark重要數據結構

說起來有一些慚愧&#xff0c;研究wireshark有一段時間了&#xff0c;但是對源代碼的分析卻至今沒有什么進展。。。 最初想要研究wireshark是因為我的開題是基于wireshark來做的。 現在有很多抓包工具&#xff0c;wireshark的優勢在于完全開源&#xff0c;分析功能強大&#xf…

C語言寫數據庫(二)

簡單的實現增刪查改的操作后&#xff0c;實現了一個先讀寫其中一個表的某兩項內容&#xff0c;再把相關字符段寫入到另外一張表中去。涉及到查詢和插入兩個步驟。 其中還涉及到漢字的讀寫和插入&#xff0c;會有字符的操作產生亂碼。所以要先保證mysql的漢字字符編碼&#xff0…

wireshark源代碼分析

各位親&#xff0c;不是我不想回復你們的問題。是我也不了解。不能誤導。希望大家相互幫助。看看能否幫那些提問的小盆友們回復一下呢&#xff1f; 這些都是轉載的&#xff0c;如果實在沒有辦法&#xff0c;可以打開鏈接到原作者哪里去提問試試看。。。 經過多次嘗試&#xf…

C語言寫數據庫(三)

遇到的問題以及解決思路方法 1.外部導入數據庫文件 進入mysql&#xff0c;創建數據庫sh_robot source /home/exbot/sh_robot.sql 查看數據庫編碼格式 show variables like “%char%”; 2.數據庫插入操作 進入相關數據庫&#xff1a;use 數據庫名&#xff1b; 查詢存在該表是否存…

Makefile(一)

在一個文件夾中建一個c文件 //main.c #include<stdio.h> int main() {printf("version 1.0");return 0; } 在當前目錄下編寫makefile文件 //makefile: test : main.o //一種依賴關系聲明&#xff0c;生成test可執行程序需要以來main.o文件gcc -o test main.…

Defunct進程 僵尸進程

在測試基于 DirectFBGstreamer 的視頻聯播系統的一個 Demo 的時候&#xff0c;其中大量使用 system 調用的語句&#xff0c;例如在 menu 代碼中的 system("./play") &#xff0c;而且多次執行&#xff0c;這種情況下&#xff0c;在 ps -ef 列表中出現了大量的 defunc…

make文件基礎用法

參照&#xff1a;https://www.jianshu.com/p/0b2a7cb9a469 創建工作目錄&#xff0c;包含一下文件 main.cperson.cb.hc.h/*** c.h ***/ //this is c.h /*** b.h ***/ //this is b.h /*** main.c ***/ #include<stdio.h> //#include"a1.h" //#include"b.h&…

一個Linux下C線程池的實現(轉)

1.線程池基本原理 在傳統服務器結構中, 常是 有一個總的 監聽線程監聽有沒有新的用戶連接服務器, 每當有一個新的 用戶進入, 服務器就開啟一個新的線程用戶處理這 個用戶的數據包。這個線程只服務于這個用戶 , 當 用戶與服務器端關閉連接以后, 服務器端銷毀這個線程。然而頻繁地…

二維數組作為函數參數

#include<stdio.h> //#include<> //二位數組作為函數參數時&#xff0c;可以不指定第一個下標 void print_buf(int (*p)[3],int a,int b) //void print_buf(int p[][3],int a,int b) {int i,j;for(i 0 ; i < a; i){for(j 0; j < b; j){printf("p[%…

mystrcat

#include<stdio.h> //如果一個數組做為函數的形參傳遞&#xff0c;那么數組可以在被調用的函數中修改 //有時候不希望這個事發生&#xff0c;所以對形參采用const參數 //size_t strlen(const char *s); //strcpy(char* s1,const char* s2); void mystrcat(char *s1,cons…

關于非阻塞的recv的時候返回的處理

注意recv&#xff08;&#xff09;如果讀到數據為0&#xff0c;那么就表示文件結束了&#xff0c;如果在讀的過程中遇到了中斷那么會返回-1&#xff0c;同時置errno為EINTR。 因此判斷recv的條件&#xff1a; 如果read返回<0 如果0 表示文件結束&…

帶參程序

windows環境 #include<stdio.h>int main(int argc, char *argv[]) {printf("argc %d\n", argc);for (int i 0; i < argc; i){printf("argv[%d] %s\n",i, argv[i]);}system("pause");return 0; } windows環境下&#xff0c;帶參函數…

Ubuntu安裝mysql步驟

1.打開終端&#xff0c;輸入&#xff1a; sudo apt-get updata 輸入root用戶密碼 2.更新完畢后&#xff0c;輸入 sudo apt-get install mysql-server ubuntu14.04安裝中間會讓你設置密碼&#xff0c;輸入密碼后點擊確認(mysql123) 3.安裝結束后&#xff0c;查看端口號是否開啟 …

Pthread創建線程后必須使用join或detach釋放線程資源

這兩天在看Pthread 資料的時候&#xff0c;無意中看到這樣一句話(man pthread_detach): Either pthread_join(3) or pthread_detach() should be called for each thread that an application creates, so that system resources for the thread can be released. …

二維數組求平均值(指針的使用)

#include<stdio.h>int main() {int buf[3][5] {{1,2,3,4,5},{4,5,6,7,8},{7,8,9,10,11}};int i;int j;//求行平均值 for(i 0; i < 3; i){int sum 0;for(j 0; j < 5; j){sum (*(*(buf i) j));}printf("sum %d\n",sum/5);}//求列平均值for(i 0; i …

linux終端關閉時為什么會導致在其上啟動的進程退出?

現象 經常在linux下開發的人應該都有這樣的經驗&#xff0c;就是在終端上啟動的程序&#xff0c;在關閉終端時&#xff0c;這個程序的進程也被一起關閉了。看下面這個程序&#xff0c;為了使進程永遠運行&#xff0c;在輸出helloworld后&#xff0c;循環調用sleep&#xff1a; …

二維數組做函數參數傳遞

#include<stdio.h> //#include<> //二位數組作為函數參數時&#xff0c;可以不指定第一個下標 void print_buf(int (*p)[3],int a,int b) //void print_buf(int p[][3],int a,int b) {int i,j;for(i 0 ; i < a; i){for(j 0; j < b; j){printf("p[%…

libevent源碼深度剖析

第一章 1&#xff0c;前言 Libevent是一個輕量級的開源高性能網絡庫&#xff0c;使用者眾多&#xff0c;研究者更甚&#xff0c;相關文章也不少。寫這一系列文章的用意在于&#xff0c;一則分享心得&#xff1b;二則對libevent代碼和設計思想做系統的、更深層次的分析&#xff…