# class KEncodingDetector

###

Module | kdecore
Namespace |
Class | KEncodingDetector
Inherits |

Provides encoding detection capabilities.

Searches for encoding declaration inside raw data -- meta and xml tags. In the case it can't find it, uses heuristics for specified language.

If it finds unicode BOM marks, it changes encoding regardless of what the user has told

Intended lifetime of the object: one instance per document.

Typical use:

    QByteArray data;
    ...
    KEncodingDetector detector;
    detector.setAutoDetectLanguage(KEncodingDetector.Cyrillic);
    QString out=detector.decode(data);

Do not mix decode() with decodeWithBuffering()

Guess encoding of char array

### enums

- AutoDetectScript { None, SemiautomaticDetection, Arabic, Baltic, CentralEuropean, ChineseSimplified, ChineseTraditional, Cyrillic, Greek, Hebrew, Japanese, Korean, NorthernSaami, SouthEasternEurope, Thai, Turkish, Unicode, WesternEuropean }
- EncodingChoiceSource { DefaultEncoding, AutoDetectedEncoding, BOM, EncodingFromXMLHeader, EncodingFromMetaTag, EncodingFromHTTPHeader, UserChosenEncoding }

enum details

### methods

- KEncodingDetector ( )

  returns ctor

  Default codec is latin1 (as html spec says), EncodingChoiceSource is default, AutoDetectScript=Semiautomatic

- KEncodingDetector (codec, source, script)

  returns ctor

  Argument | Type | Default
  --- | --- | ---
  codec | QTextCodec |
  source | KEncodingDetector.EncodingChoiceSource |
  script | KEncodingDetector.AutoDetectScript | KEncodingDetector.None

  Allows to set Default codec, EncodingChoiceSource, AutoDetectScript

- analyze (data, len)

  returns bool

  Argument | Type | Default
  --- | --- | ---
  data | char |
  len | int |

  Analyze text data. Returns true if there was enough data for accurate detection

- autoDetectLanguage ( )

  returns KEncodingDetector.AutoDetectScript

- decode (data, len)

  returns QString

  Argument | Type | Default
  --- | --- | ---
  data | char |
  len | int |

  The main class method

  Calls protected analyze() only the first time of the whole object life

  Replaces all null chars with spaces.

- decode (data)

  returns QString

  Argument | Type | Default
  --- | --- | ---
  data | QByteArray |

- decodeWithBuffering (data, len)

  returns QString

  Argument | Type | Default
  --- | --- | ---
  data | char |
  len | int |

  Convenience method that uses buffering. It waits for full html head to be buffered (i.e. calls analyze every time until it returns true).

  Replaces all null chars with spaces.

  Returns Decoded data, or empty string, if there was not enough data for accurate detection

  See also flush()

- decoder ( )

  returns QTextDecoder

  Returns QTextDecoder for detected encoding

- encoding ( )

  returns char

  Convenience method. Returns mime name of detected encoding

- encodingChoiceSource ( )

  returns KEncodingDetector.EncodingChoiceSource

- errorsIfUtf8 (data, length)

  returns bool

  Argument | Type | Default
  --- | --- | ---
  data | char |
  length | int |

  Check if we are really utf8. Taken from kate

  Returns true if current encoding is utf8 and the text cannot be in this encoding

  Please somebody read http://de.wikipedia.org/wiki/UTF-8 and check this code...

- flush ( )

  returns QString

  Convenience method to be used with decodeForHtml. Flushes buffer.

  See also decodeForHtml()

- hasAutoDetectionForScript (a0)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  a0 | KEncodingDetector.AutoDetectScript |

- nameForScript (a0)

  returns QString
  static

  Argument | Type | Default
  --- | --- | ---
  a0 | KEncodingDetector.AutoDetectScript |

- processNull (data, length)

  returns bool

  Argument | Type | Default
  --- | --- | ---
  data | char |
  length | int |

  This nice method will kill all 0 bytes (or double bytes) and remember if this was a binary or not ;)

- scriptForName (lang)

  returns KEncodingDetector.AutoDetectScript
  static

  Argument | Type | Default
  --- | --- | ---
  lang | QString |

  Takes lang name _after_ it were i18n()'ed

- setAutoDetectLanguage (a0)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  a0 | KEncodingDetector.AutoDetectScript |

- setEncoding (encoding, type)

  returns bool

  Argument | Type | Default
  --- | --- | ---
  encoding | char |
  type | KEncodingDetector.EncodingChoiceSource |

  Returns true if specified encoding was recognized

- visuallyOrdered ( )

  returns bool

### enums - detail

- AutoDetectScript { None, SemiautomaticDetection, Arabic, Baltic, CentralEuropean, ChineseSimplified, ChineseTraditional, Cyrillic, Greek, Hebrew, Japanese, Korean, NorthernSaami, SouthEasternEurope, Thai, Turkish, Unicode, WesternEuropean }

  | None | - | - |
  | SemiautomaticDetection | - | - |
  | Arabic | - | - |
  | Baltic | - | - |
  | CentralEuropean | - | - |
  | ChineseSimplified | - | - |
  | ChineseTraditional | - | - |
  | Cyrillic | - | - |
  | Greek | - | - |
  | Hebrew | - | - |
  | Japanese | - | - |
  | Korean | - | - |
  | NorthernSaami | - | - |
  | SouthEasternEurope | - | - |
  | Thai | - | - |
  | Turkish | - | - |
  | Unicode | - | - |
  | WesternEuropean | - | - |

- EncodingChoiceSource { DefaultEncoding, AutoDetectedEncoding, BOM, EncodingFromXMLHeader, EncodingFromMetaTag, EncodingFromHTTPHeader, UserChosenEncoding }

  | DefaultEncoding | - | - |
  | AutoDetectedEncoding | - | - |
  | BOM | - | - |
  | EncodingFromXMLHeader | - | - |
  | EncodingFromMetaTag | - | - |
  | EncodingFromHTTPHeader | - | - |
  | UserChosenEncoding | - | - |