Sovrascrivere una stringa con uno o più caratteri mediante le funzioni esterne del API di Libglib-2.0

Da Gambas-it.org - Wikipedia.

E' possibile sovrascrivere una parte ben determinata di una stringa con uno o più caratteri, a cominciare da una specificata posizione, utilizzando alcune funzioni della libreria esterna libglib-2.0 .

Sarà necessario aver installato nel sistema e richiamare nell'applicazione Gambas la libreria: "libglib-2.0.so"


Mostriamo un esempio, nel quale una stringa preesistente verrà sovrascritta con la parola "GAMBAS" a partire dal 5° carattere (num. indice 4) della stringa medesima:

Public Struct GString
  gstr As Pointer
  len As Long
  allocated_len As Long
End Struct

Library "libglib-2.0"

' GString * g_string_new (const gchar *init)
' Creates a new GString, initialized with the given string.
Private Extern g_string_new(init As String) As GString

' GString * g_string_overwrite (GString *string, gsize pos, const gchar *val)
' Overwrites part of a string, lengthening it if necessary.
Private Extern g_string_overwrite(gstr As GString, gpos As Integer, gval As String) As GString

' gchar * g_string_free (GString *string, gboolean free_segment)
' Frees the memory allocated for the GString.
Private Extern g_string_free(gstr As GString, free_segment As Boolean) As String


Public Sub Main()

 Dim s As String
 Dim gs As New GString
 
  s = "abcde fghilm nopqrstuvz"
 
  gs = g_string_new(s)
      
  gs = g_string_overwrite(gs, 4, "GAMBAS")
   
  Print String@(Pointer@(gs.gstr))
   
' Va in chiusura liberando memoria occupata dalla Struttura "GString":
  g_string_free(gs, True)
 
End


Sovrascrivere una stringa con una determinata quantità di caratteri di un'altra stringa

E' anche possibile sovrascrivere una stringa originaria con un numero determinato di caratteri, iniziando da sinistra, facendit parte di un'altra stringa.

Mostriamo un esempio, nel quale una stringa verrà sovrascritta a partire dalla posizione di indice 6 con i soli primi tre caratteri della stringa "GAMBAS":

Public Struct GString
  gstr As Pointer
  len As Long
  allocated_len As Long
End Struct

Library "libglib-2.0"

' GString * g_string_new (const gchar *init)
' Creates a new GString, initialized with the given string.
Private Extern g_string_new(init As String) As GString

' GString * g_string_overwrite_len (GString *string, gsize pos, const gchar *val, len As Long)
' Overwrites part of a string, lengthening it if necessary.
Private Extern g_string_overwrite_len(gstr As GString, gpos As Integer, gval As String) As GString

' gchar * g_string_free (GString *string, gboolean free_segment)
' Frees the memory allocated for the GString.
Private Extern g_string_free(gstr As GString, free_segment As Boolean) As String


Public Sub Main()

 Dim s As String
 Dim gs As New GString
 
  s = "abcde fghilm nopqrstuvz"
 
  gs = g_string_new(s)
      
  gs = g_string_overwrite_len(gs, 6, "GAMBAS", 3)
   
  Print String@(Pointer@(gs.gstr))
   
' Va in chiusura liberando memoria occupata dalla Struttura "GString":
  g_string_free(gs, True)
 
End