Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

Glib String Handling

Posted on July 27, 2012

glib provides a number of functions for string handling; some are unique to glib, and some solve portability concerns. They all interoperate nicely with the glib memory allocation routines.

For those interested in a better string than gchar*, there’s also a GString type. It isn’t covered in this book, but documentation is available at http://www.gtk.org/.

[code lang=”cpp”]#include <glib.h>

gint g_snprintf(gchar* buf, gulong n, const gchar* format, …);
gint g_strcasecmp(const gchar* s1, const gchar* s2);
gint g_strncasecmp(const gchar* s1, const gchar* s2, guint n);
[/code]
Figure 2-7. Portability Wrappers

One of the annoying things about C is that it provides the crash-causing, security- hole-creating, generally evil sprintf(), but the relatively safe and widely implemented snprintf() is a vendor extension. g_snprintf() wraps native snprintf() on platforms that have it, and provides an implementation on those that don’t. So you can say goodbye to sprintf() forever. Even better: classically, snprintf() does not guarantee that it will NULL-terminate the buffer it fills. g_snprintf() does.

g_strcasecmp() and g_strncasecmp() perform a case-insensitive comparison of two strings, optionally with a maximum length. strcasecmp() is available on many platforms but not universally, so using glib instead is advisable.

The functions in Figure 2-8 modify a string in-place: the first two convert the string to lowercase or uppercase, respectively, while g_strreverse() reverses its characters. g_strchug() and g_strchomp() “chug” the string (remove leading spaces), or “chomp” it (remove trailing spaces). These last two return the string, in addition to modifying it in-place; in some cases it may be convenient to use the return value. There is a macro, g_strstrip(), which combines both functions to remove both leading and trailing spaces; it is used just as the individual functions are.

[code lang=”cpp”]
#include <glib.h>
void g_strdown(gchar* string);
void g_strup(gchar* string);
void g_strreverse(gchar* string);
gchar* g_strchug(gchar* string);
gchar* g_strchomp(gchar* string);
[/code]
Figure 2-8. In-place string modifications

Figure 2-9 shows a few more semi-standard functions glib wraps. g_strtod is like strtod()—it converts string nptr to a double—with the exception that it will also at- tempt to convert the double in the “C” locale if it fails to convert it in the user ’s default locale. *endptr is set to the first unconverted character, i.e. any text after the number representation. If conversion fails, *endptr is set to nptr. endptr may be NULL, causing it to be ignored.

g_strerror() and g_strsignal() are like their non-g_ equivalents, but portable. (They return a string representation for an errno or a signal number.)
[code lang=”cpp”]
#include <glib.h>

gdouble g_strtod(const gchar* nptr, gchar** endptr);
gchar* g_strerror(gint errnum);
gchar* g_strsignal(gint signum);
[/code]
Figure 2-9. String Conversions

Figure 2-10 shows glib’s rich array of functions for allocating strings. Unsurprisingly, g_strdup() and g_strndup() produce an allocated copy of str or the first n characters of str. For consistency with the glib memory allocation functions, they return NULL if passed a NULL pointer. The printf() variants return a formatted string. g_strescape escapes any \ characters in its argument by inserting another \ before them, returning the escaped string. g_strnfill() returns a string of size length filled with fill_char. g_strdup_printf() deserves a special mention; it is a simpler way to handle this common piece of code:
[code lang=”cpp”]
gchar* str = g_malloc(256);
g_snprintf(str, 256, "%d printf-style %s", 1, "format");
[/code]

Instead you could say this, and avoid having to figure out the proper length of the buffer to boot:
[code lang=”cpp”]
gchar* str = g_strdup_printf("%d printf-style %s", 1, "format");
[/code]

[code lang=”cpp”]
#include <glib.h>
gchar* g_strdup(const gchar* str);
gchar* g_strndup(const gchar* format, guint n);
gchar* g_strdup_printf(const gchar* format, …);
gchar* g_strdup_vprintf(const gchar* format, va_list args);
gchar* g_strescape(gchar* string);
gchar* g_strnfill(guint length, gchar fill_char);
[/code]
Figure 2-10. Allocating Strings

glib provides some convenient functions for concatenating strings, shown in Figure
2-11. g_strconcat() returns a newly-allocated string created by concatenating each
of the strings in the argument list. The last argument must be NULL, so g_strconcat()
knows when to stop. g_strjoin() is similar, but separator is inserted between each
string. If separator is NULL, no separator is used.

[code lang=”cpp”]
#include <glib.h>

gchar* g_strconcat(const gchar* string1, …);
gchar* g_strjoin(const gchar* separator, …);
[/code]
Figure 2-11. Concatenating strings

Finally, Figure 2-12 summarizes a few routines which manipulate NULL-terminated arrays of strings. g_strsplit() breaks string at each delimiter, returning a newly- allocated array. g_strjoinv() concatenates each string in the array with an optional separator, returning an allocated string. g_strfreev() frees each string in the array and then the array itself.
[code lang=”cpp”]
#include <glib.h>

gchar** g_strsplit(const gchar* string, const gchar* delimiter, gint max_tokens);
gchar* g_strjoinv(const gchar* separator, gchar** str_array);
void g_strfreev(gchar** str_array);
[/code]
Figure 2-12. Manipulating NULL-terminated string vectors

Source: Havoc Pennington. 1999. GTK Gnome Application Development. New York: New Riders Publishing

Terbaru

  • Cara Kelola Auto-Posting Semua Media Sosial Kalian Pakai Metricool
  • Studi Kasus Sukses Instagram Maria Wendt Dapat 12 Juta View Instagram Per Bulan
  • ZenBook S16, Vivobook Pro 15 OLED, ProArt PX13, dan ROG Zephyrus G14, Laptop Bagus dengan Layar OLED!
  • Caranya Ngebangun Website Directory dengan Traffic Tinggi dalam Seminggu!
  • Cara Mengembangkan Channel YouTube Shorts Tanpa Wajah
  • Inilah Cara Menghitung Diskon Baju Lebaran Biar Nggak Bingung Saat Belanja di Mall!
  • Cara Jitu Ngebangun Bisnis SaaS di Era AI Pakai Strategi Agentic Workflow
  • Inilah Rincian Gaji Polri Lulusan Baru 2026, Cek Perbedaan Jalur Akpol, Bintara, dan Tamtama Sebelum Daftar!
  • Inilah 5 Channel YouTube Membosankan yang Diam-diam Menghasilkan Banyak Uang
  • Inilah Cara Pakai Google Maps Offline Biar Mudik Lebaran 2026 Nggak Nyasar Meski Tanpa Sinyal!
  • Inilah Alasan Mahkamah Agung Tolak Kasasi Google, Denda Rp202,5 Miliar Resmi Menanti Akibat Praktik Monopoli
  • Inilah Cara Daftar dan Syarat SPMB SMK Boarding Jawa Tengah 2026, Sekolah Gratis Sampai Lulus!
  • Inilah Daftar Sekolah Kedinasan 2026 untuk Lulusan SMK, Bisa Kuliah Gratis dan Berpeluang Besar Langsung Jadi CPNS!
  • Inilah Pajak TER: Skema Baru PPh 21 yang Nggak Bikin Pusing, Begini Cara Hitungnya!
  • Inilah Jadwal Resmi Jam Buka Tol Jogja-Solo Segmen Prambanan-Purwomartani Saat Mudik Lebaran 2026
  • Inilah Cara Mendapatkan Witherbloom di Fisch Roblox, Rahasia Menangkap Ikan Paling Sulit di Toxic Grove!
  • Kenapa Indomart Point Bisa Kalahkan Bisnis Kafe?
  • Inilah Cara Mendapatkan Rotten Seed di Fisch Roblox, Lokasi Rahasia di Toxic Grove Buat Unlock Toxic Lotus!
  • Inilah Cara Zakat Crypto Kalian Bisa Jadi Pengurang Pajak Berdasarkan Aturan Resmi Pemerintah!
  • Inilah Perbandingan Airwallex vs Payoneer 2026: Jangan Sampai Profit Kalian Ludes Gara-Gara Biaya Admin!
  • Inilah Roadmap 7 Tahap Bangun Bisnis Digital dari Nol Biar Nggak Cuma Putar-Putar di Tempat!
  • Inilah Cara Tetap Gajian dari YouTube Meski View Masih Ratusan, Penasaran?
  • Inilah Alasan Akun TikTok Affiliate GMV 270 Juta Kena Banned Permanen!
  • Inilah Bahaya Astute Beta Server APK, Jangan Sembarang Klik Link Download FF Kipas 2026!
  • Inilah Bahaya Nonton Film di LK21 dan IndoXXI, Awas Data Pribadi dan Saldo Rekening Kalian Bisa Ludes!
  • Inilah Kronologi & Video Lengkap Kasus Sejoli Tambelangan Sampang Viral, Ternyata Gini Awal Mulanya!
  • Inilah Alasan Kenapa Koin Nego Neko Shopee Nggak Bisa Dipakai Bayar Full dan Cara Rahasia Dapetinnya!
  • Inilah Cara Menjawab Pertanyaan Apakah di Sekolahmu Sudah Ada IFP/PID dengan Benar dan Profesional
  • Inilah Fakta Isu Roblox Diblokir di Indonesia 2026, Benarkah Akan Ditutup Total?
  • Inilah Penyebab dan Cara Mengatasi FF Kipas My ID Verify UID Biar Akun Tetap Aman
  • How to Recover Lost Windows Passwords with a Decryptor Tool
  • How to Fix Python Not Working in VS Code Terminal: A Troubleshooting Guide
  • Game File Verification Stuck at 0% or 99%: What is it and How to Fix the Progress Bar?
  • Why Does PowerPoint Underline Hyperlinks? Here is How to Remove Them
  • AI Bug Hunting with Semgrep
  • Create Your Own Netflix-Style Documentaries Using AIQORA in Minutes!
  • How to Build a Super Chatbot with RAG Gemini Embbeding & Claude Code
  • How to Do Professional AI Prompting in Nano Banana 2
  • How to Create Agent & Automation in Minutes with Sim AI
  • Claude Code Tips: Don’t Overuse SKILL.md!
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025

©2026 emka.web.id | Design: Newspaperly WordPress Theme