Autore Topic: Rilevamento di un carattere non-UTF in una stringa  (Letto 393 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.292
  • Ne mors quidem nos iunget
    • Mostra profilo
Rilevamento di un carattere non-UTF in una stringa
« il: 29 Marzo 2014, 01:17:25 »
Vi riporto questa discussione apparsa nella M.L. ufficiale:

" I get an occasional* error with downloaded data which results in a
 postgresql update failure with the following error "ERR: Cannot create
 record: ERROR:  invalid byte sequence for encoding "UTF8": 0xc2". I know
 how to fix that as long as I can detect the bad char in the string. That
 is what I dont know, in fact I haven't got the foggiest clue.

 The input data is split out from a text file downloaded via FTP. The bad
 data lines come from a particular source but only occur occasionally.
 They always occur at the same "place" in the parsed file (i.e. a "Title"
 field, which is a moderately lengthed string extracted using a Scan()
 function.) They always appear at the same place in the field (i.e. at
 the very end of the field.

 So, how can I test if Right(sTitle,1) = NotARealCharacter?

 tia
 Bruce
"


" This is missing in Gambas. A String.IsValid() method that would tell you
if a string is valid UTF-8.

At the moment, you must validate your string manually.

Or maybe there is a trick:

Codice: gambas [Seleziona]
Conv$(MyString, "UTF-8", "UCS-4LE")


Conv$() may force a validation of MyString (I'm not sure as this is done
by the internal iconv GNU library). So if Conv$() raises an error, it
must mean that MyString is not a valid UTF-8 string.

Regards,

--
Benoît Minisini
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline Gianluigi

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 4.158
  • Tonno verde
    • Mostra profilo
Re: Rilevamento di un carattere non-UTF in una stringa
« Risposta #1 il: 29 Marzo 2014, 14:42:27 »
Vi riporto questa discussione apparsa nella M.L. ufficiale:

"
...

" This is missing in Gambas. A String.IsValid() method that would tell you
if a string is valid UTF-8.
...

Regards,
--
Benoît Minisini
"

Però poi mi sembra che abbia smentito se stesso...

IsValid() esiste!

 :ciao:
nuoto in attesa del bacio di una principessa che mi trasformi in un gambero azzurro

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.292
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: Rilevamento di un carattere non-UTF in una stringa
« Risposta #2 il: 29 Marzo 2014, 16:21:51 »
IsValid() esiste!


Bravo Gianluigi !  :)

ed infatti Minisini si è corretto !

" Oops. Actually String.IsValid() already exists. :-)

So just can use it!

--
Benoît Minisini
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »