Differenze tra le versioni di "Conoscere il numero di occorrenze in una stringa con la funzione esterna PyUnicode Count() della libreria libpython3"

Da Gambas-it.org - Wikipedia.
Riga 1: Riga 1:
 
La libreria ''libpython3'' è un interfaccia in C di funzioni appartenenti alle risorse di Python.
 
La libreria ''libpython3'' è un interfaccia in C di funzioni appartenenti alle risorse di Python.
  
La funzione ''PyUnicode Count( )'' consente di sapere quante volte una sub-stringa ricorre all'interno di una stringa.
+
La funzione "PyUnicode_Count()" consente di sapere quante volte una sub-stringa ricorre all'interno di una stringa.
 
 
Per poter fruire in Gambas di tale funzione esterna, è necessario richiamare la libreria dinamica condivisa: "''libpython3.6m.so.1.0''"
 
  
 +
Per poter fruire in Gambas di tale funzione esterna, è necessario richiamare la libreria condivisa: "''libpython3.8.so.1.0'' "
  
 
Mostriamo un semplice esempio pratico:
 
Mostriamo un semplice esempio pratico:
  Library "libpython3.6m:1.0"
+
  Library "libpython3.8:1.0"
 
   
 
   
 
  <FONT Color=gray>' ''(PyObject*) PyUnicode_FromString(const char *u)''
 
  <FONT Color=gray>' ''(PyObject*) PyUnicode_FromString(const char *u)''
Riga 24: Riga 23:
 
   Dim i As Integer
 
   Dim i As Integer
 
      
 
      
  s = "testo testo abcde testo testo"
+
  s = "testo testo abcde testo testo"
 
      
 
      
  p = PyUnicode_FromString(s)
+
  p = PyUnicode_FromString(s)
 
      
 
      
  su = PyUnicode_FromString("st")
+
  su = PyUnicode_FromString("st")
 
      
 
      
  i = PyUnicode_Count(p, su, 0, String.Len(s))
+
  i = PyUnicode_Count(p, su, 0, String.Len(s))
  Print i
+
  Print i
 
    
 
    
 
  '''End'''
 
  '''End'''
 
  
  
  
 
=Riferimenti=
 
=Riferimenti=
* https://docs.python.org/3.6/c-api/unicode.html
+
* https://docs.python.org/3.8/c-api/unicode.html

Versione delle 18:04, 28 giu 2022

La libreria libpython3 è un interfaccia in C di funzioni appartenenti alle risorse di Python.

La funzione "PyUnicode_Count()" consente di sapere quante volte una sub-stringa ricorre all'interno di una stringa.

Per poter fruire in Gambas di tale funzione esterna, è necessario richiamare la libreria condivisa: "libpython3.8.so.1.0 "

Mostriamo un semplice esempio pratico:

Library "libpython3.8:1.0"

' (PyObject*) PyUnicode_FromString(const char *u)
' Create a Unicode object from an UTF-8 encoded null-terminated char buffer u.
Private Extern PyUnicode_FromString(u As String) As Pointer

' (Py_ssize_t) PyUnicode_Count(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end)
' Count the number of occurrences of substr in str[start:end].
Private Extern PyUnicode_Count(strp As Pointer, substr As Pointer, start As Long, endl As Long) As Long


Public Sub Main()
 
 Dim p, su As Pointer
 Dim s As String
 Dim i As Integer
   
 s = "testo testo abcde testo testo"
   
 p = PyUnicode_FromString(s)
   
 su = PyUnicode_FromString("st")
   
 i = PyUnicode_Count(p, su, 0, String.Len(s))
 Print i
  
End


Riferimenti