Exemplo n.º 1
0
void Test::TestStringiter() {
    const char testChars[] = "Now is the time for all good men to come "
        "to the aid of their country.";

    UnicodeString testString(testChars,"");
    const UChar *testText    = testString.getTerminatedBuffer();
    
    StringCharacterIterator iter(testText, u_strlen(testText));
    StringCharacterIterator* test2 = (StringCharacterIterator*)iter.clone();
    
    if (iter != *test2 ) {
        printf("clone() or equals() failed: Two clones tested unequal\n");
    }
    
    UnicodeString result1, result2;
    // getting and comparing the text within the iterators
    iter.getText(result1);
    test2->getText(result2);
    if (result1 != result2) {
        printf("getText() failed\n");
    }

    u_fprintf(out, "Backwards: ");

    UChar c = iter.last();
    int32_t i = iter.endIndex();

    printUChar(c);
    i--; // already printed out the last char 

    if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) {
        printf("startIndex() or endIndex() failed\n");
    }
    
    // Testing backward iteration over a range...
    do {
        if (c == CharacterIterator::DONE) {
            printf("Iterator reached end prematurely\n");
        }
        else if (c != testText[i]) {
            printf("Character mismatch at position %d\n" + i);
        }
        if (iter.current() != c) {
            printf("current() isn't working right\n");
        }
        if (iter.getIndex() != i) {
            printf("getIndex() isn't working right [%d should be %d]\n", iter.getIndex(), i);
        }
        if (c != CharacterIterator::DONE) {
            c = iter.previous();
            i--;
        }

        u_fprintf(out, "|");
        printUChar(c);
    } while (c != CharacterIterator::DONE);

    u_fprintf(out, "\n");
    delete test2;
}
Exemplo n.º 2
0
UErrorCode convsample_13()
{
  printf("\n\n==============================================\n"
         "Sample 13: C: simple Big5 -> unicode conversion, char at a time\n");


  const char sourceChars[] = { 0x7a, 0x68, 0x3d, (char)0xa4, (char)0xa4, (char)0xa4, (char)0xe5, (char)0x2e };
  //  const char sourceChars[] = { 0x7a, 0x68, 0x3d, 0xe4, 0xb8, 0xad, 0xe6, 0x96, 0x87, 0x2e };
  const char *source, *sourceLimit;
  UChar32 target;
  UErrorCode status = U_ZERO_ERROR;
  UConverter *conv = NULL;
  int32_t srcCount=0;
  int32_t dstCount=0;
  
  srcCount = sizeof(sourceChars);

  conv = ucnv_open("Big5", &status);
  U_ASSERT(status);

  source = sourceChars;
  sourceLimit = sourceChars + sizeof(sourceChars);

  // **************************** START SAMPLE *******************


  printBytes("src",source,sourceLimit-source);

  while(source < sourceLimit)
  {
    puts("");
    target = ucnv_getNextUChar (conv,
                                &source,
                                sourceLimit,
                                &status);
    
    //    printBytes("src",source,sourceLimit-source);
    U_ASSERT(status);
    printUChar(target);
    dstCount++;
  }
  
  
  // ************************** END SAMPLE *************************
  
  printf("src=%d bytes, dst=%d uchars\n", srcCount, dstCount);
  ucnv_close(conv);

  return U_ZERO_ERROR;
}
Exemplo n.º 3
0
void Test::TestUChariter() {
    const char testChars[] = "Now is the time for all good men to come "
            "to the aid of their country.";

    UnicodeString testString(testChars,"");
    const UChar *testText = testString.getTerminatedBuffer();

    UCharCharacterIterator iter(testText, u_strlen(testText));
    UCharCharacterIterator* test2 = (UCharCharacterIterator*)iter.clone();
    
	u_fprintf(out, "testText = %s", testChars);
    
    if (iter != *test2 ) {
        printf("clone() or equals() failed: Two clones tested unequal\n");
    }
    
    UnicodeString result1, result2;
    // getting and comparing the text within the iterators
    iter.getText(result1);
    test2->getText(result2);
    if (result1 != result2) {
            printf("iter.getText() != clone.getText()\n");
    } 
    
    u_fprintf(out, "\n");

	// Demonstrates seeking forward using the iterator.
    u_fprintf(out, "Forward  = ");
    
    UChar c = iter.first();
    printUChar(c);    // The first char
    int32_t i = 0;
    
    if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) {
        printf("startIndex() or endIndex() failed\n");
    }
    
    
    // Testing forward iteration...
    do {
        if (c == CharacterIterator::DONE && i != u_strlen(testText)) {
            printf("Iterator reached end prematurely");
        }
        else if (c != testText[i]) {
            printf("Character mismatch at position %d\n" + i);
        }
        if (iter.current() != c) {
            printf("current() isn't working right");
        }
        if (iter.getIndex() != i) {
                        printf("getIndex() isn't working right\n");
        }
        if (c != CharacterIterator::DONE) {
            c = iter.next();
            i++;
        }
        
        u_fprintf(out, "|");
        printUChar(c);
                
    } while (c != CharacterIterator::DONE);        
    
    delete test2;
    u_fprintf(out, "\n");
}