~ubuntu-branches/ubuntu/vivid/tidy/vivid-updates

« back to all changes in this revision

Viewing changes to src/utf8.c

  • Committer: Bazaar Package Importer
  • Author(s): Jason Thomas
  • Date: 2008-01-20 21:46:03 UTC
  • mfrom: (0.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080120214603-oqicq5jwr1exrm55
Tags: 20080116cvs-2
* debian/control: build depends on xsltproc
  (closes: #461608)
* debian/tidy.preinst,postinst: add code to move old config file
  (closes: #461623)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* utf8.c -- convert characters to/from UTF-8
2
2
 
3
 
  (c) 1998-2004 (W3C) MIT, ERCIM, Keio University
 
3
  (c) 1998-2007 (W3C) MIT, ERCIM, Keio University
4
4
  See tidy.h for the copyright notice.
5
5
 
6
6
  CVS Info :
7
7
 
8
 
    $Author: terry_teague $ 
9
 
    $Date: 2004/08/02 02:32:36 $ 
10
 
    $Revision: 1.7 $ 
 
8
    $Author: arnaud02 $ 
 
9
    $Date: 2007/05/30 16:47:31 $ 
 
10
    $Revision: 1.10 $ 
11
11
 
12
12
  Uses public interfaces to abstract input source and output
13
13
  sink, which may be user supplied or either FILE* or memory
31
31
*/
32
32
 
33
33
#include "tidy.h"
 
34
#include "forward.h"
34
35
#include "utf8.h"
35
36
 
36
37
/* 
166
167
    {0x100000, 0x10FFFF, 4, {0xF4, 0xF4, 0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}} 
167
168
};
168
169
 
169
 
int DecodeUTF8BytesToChar( uint* c, uint firstByte, ctmbstr successorBytes,
170
 
                           TidyInputSource* inp, int* count )
 
170
int TY_(DecodeUTF8BytesToChar)( uint* c, uint firstByte, ctmbstr successorBytes,
 
171
                                TidyInputSource* inp, int* count )
171
172
{
172
173
    byte tempbuf[10];
173
174
    byte *buf = &tempbuf[0];
241
242
            if ( !buf[i] || (buf[i] & 0xC0) != 0x80 )
242
243
            {
243
244
                hasError = yes;
244
 
                bytes = i;
 
245
                bytes = i+1;
245
246
                break;
246
247
            }
247
248
            n = (n << 6) | (buf[i] & 0x3F);
258
259
            if ( b == EOF || (buf[i] & 0xC0) != 0x80 )
259
260
            {
260
261
                hasError = yes;
261
 
                bytes = i;
 
262
                bytes = i+1;
262
263
                if ( b != EOF )
263
264
                    inp->ungetByte( inp->sourceData, buf[i] );
264
265
                break;
339
340
    return 0;
340
341
}
341
342
 
342
 
int EncodeCharToUTF8Bytes( uint c, tmbstr encodebuf,
343
 
                           TidyOutputSink* outp, int* count )
 
343
int TY_(EncodeCharToUTF8Bytes)( uint c, tmbstr encodebuf,
 
344
                                TidyOutputSink* outp, int* count )
344
345
{
345
346
    byte tempbuf[10] = {0};
346
347
    byte* buf = &tempbuf[0];
438
439
/* return one less than the number of bytes used by the UTF-8 byte sequence */
439
440
/* str points to the UTF-8 byte sequence */
440
441
/* the Unicode char is returned in *ch */
441
 
uint GetUTF8( ctmbstr str, uint *ch )
 
442
uint TY_(GetUTF8)( ctmbstr str, uint *ch )
442
443
{
443
444
    uint n;
444
445
    int bytes;
449
450
    
450
451
    /* first byte "str[0]" is passed in separately from the */
451
452
    /* rest of the UTF-8 byte sequence starting at "str[1]" */
452
 
    err = DecodeUTF8BytesToChar( &n, str[0], str+1, NULL, &bytes );
 
453
    err = TY_(DecodeUTF8BytesToChar)( &n, str[0], str+1, NULL, &bytes );
453
454
    if (err)
454
455
    {
455
456
#if 1 && defined(_DEBUG)
463
464
}
464
465
 
465
466
/* store char c as UTF-8 encoded byte stream */
466
 
tmbstr PutUTF8( tmbstr buf, uint c )
 
467
tmbstr TY_(PutUTF8)( tmbstr buf, uint c )
467
468
{
468
469
    int err, count = 0;
469
470
        
470
 
    err = EncodeCharToUTF8Bytes( c, buf, NULL, &count );
 
471
    err = TY_(EncodeCharToUTF8Bytes)( c, buf, NULL, &count );
471
472
    if (err)
472
473
    {
473
474
#if 1 && defined(_DEBUG)
484
485
    return buf;
485
486
}
486
487
 
487
 
Bool    IsValidUTF16FromUCS4( tchar ucs4 )
 
488
Bool    TY_(IsValidUTF16FromUCS4)( tchar ucs4 )
488
489
{
489
490
  return ( ucs4 <= kMaxUTF16FromUCS4 );
490
491
}
491
492
 
492
 
Bool    IsHighSurrogate( tchar ch )
 
493
Bool    TY_(IsHighSurrogate)( tchar ch )
493
494
{
494
495
    return ( ch >= kUTF16HighSurrogateBegin && ch <= kUTF16HighSurrogateEnd );
495
496
}
496
 
Bool    IsLowSurrogate( tchar ch )
 
497
Bool    TY_(IsLowSurrogate)( tchar ch )
497
498
{
498
499
    return ( ch >= kUTF16LowSurrogateBegin && ch <= kUTF16LowSurrogateEnd );
499
500
}
500
501
 
501
 
tchar   CombineSurrogatePair( tchar high, tchar low )
 
502
tchar   TY_(CombineSurrogatePair)( tchar high, tchar low )
502
503
{
503
 
    assert( IsHighSurrogate(high) && IsLowSurrogate(low) );
 
504
    assert( TY_(IsHighSurrogate)(high) && TY_(IsLowSurrogate)(low) );
504
505
    return ( ((low - kUTF16LowSurrogateBegin) * 0x400) + 
505
506
             high - kUTF16HighSurrogateBegin + 0x10000 );
506
507
}
507
508
 
508
 
Bool   SplitSurrogatePair( tchar utf16, tchar* low, tchar* high )
 
509
Bool   TY_(SplitSurrogatePair)( tchar utf16, tchar* low, tchar* high )
509
510
{
510
 
    Bool status = ( IsValidCombinedChar( utf16 ) && high && low );
 
511
    Bool status = ( TY_(IsValidCombinedChar)( utf16 ) && high && low );
511
512
    if ( status )
512
513
    {
513
514
        *low  = (utf16 - kUTF16SurrogatesBegin) / 0x400 + kUTF16LowSurrogateBegin;
516
517
    return status;
517
518
}
518
519
 
519
 
Bool    IsValidCombinedChar( tchar ch )
 
520
Bool    TY_(IsValidCombinedChar)( tchar ch )
520
521
{
521
522
    return ( ch >= kUTF16SurrogatesBegin &&
522
523
             (ch & 0x0000FFFE) != 0x0000FFFE &&
523
524
             (ch & 0x0000FFFF) != 0x0000FFFF );
524
525
}
525
526
 
526
 
Bool    IsCombinedChar( tchar ch )
 
527
Bool    TY_(IsCombinedChar)( tchar ch )
527
528
{
528
529
    return ( ch >= kUTF16SurrogatesBegin );
529
530
}
 
531
 
 
532
/*
 
533
 * local variables:
 
534
 * mode: c
 
535
 * indent-tabs-mode: nil
 
536
 * c-basic-offset: 4
 
537
 * eval: (c-set-offset 'substatement-open 0)
 
538
 * end:
 
539
 */