123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
/*******************************************************************************

        copyright:      Copyright (c) 2007 Peter Triller. All rights reserved

        license:        BSD style: $(LICENSE)

        version:        Initial release: Sept 2007

        authors:        Peter

        Provides case mapping Functions for Unicode Strings. As of now it is
        only 99 % complete, because it does not take into account Conditional
        case mappings. This means the Greek Letter Sigma will not be correctly
        case mapped at the end of a Word, and the Locales Lithuanian, Turkish
        and Azeri are not taken into account during Case Mappings. This means
        all in all around 12 Characters will not be mapped correctly under
        some circumstances.

        ICU4j also does not handle these cases at the moment.

        Unittests are written against output from ICU4j

        This Module tries to minimize Memory allocation and usage. You can
        always pass the output buffer that should be used to the case mapping
        function, which will be resized if necessary.

*******************************************************************************/

module tango.text.Unicode;

private import tango.text.UnicodeData;
private import tango.text.convert.Utf;



/**
 * Converts an Utf8 String to Upper case
 *
 * Params:
 *     input = String to be case mapped
 *     output = this output buffer will be used unless too small
 * Returns: the case mapped string
 */
/+deprecated char[] blockToUpper(char[] input, char[] output = null, dchar[] working = null) {

    // ?? How much preallocation ?? This is worst case allocation
    if (working is null)
        working.length = input.length;

    uint produced = 0;
    size_t ate;
    uint oprod = 0;
    foreach(dchar ch; input) {
        // TODO Conditional Case Mapping
        UnicodeData *d = getUnicodeData(ch);
        if(d !is null && (d.generalCategory & UnicodeData.GeneralCategory.SpecialMapping)) {
            SpecialCaseData *s = getSpecialCaseData(ch);
            debug {
                assert(s !is null);
            }
            if(s.upperCaseMapping !is null) {
                // To speed up, use worst case for memory prealocation
                // since the length of an UpperCaseMapping list is at most 4
                // Make sure no relocation is made in the toString Method
                // better allocation algorithm ?
                auto len = s.upperCaseMapping.length;
                if(produced + len >= working.length)
                    working.length = working.length + working.length / 2 +  len;
                oprod = produced;
                produced += len;
                working[oprod..produced] = s.upperCaseMapping;
                continue;
            }
        }
        // Make sure no relocation is made in the toString Method
        if(produced + 1 >= output.length)
            working.length = working.length + working.length / 2 + 1;
        working[produced++] =  d is null ? ch:d.simpleUpperCaseMapping;
    }
    return toString(working[0..produced],output);
}+/



/**
 * Converts an Utf8 String to Upper case
 *
 * Params:
 *     input = String to be case mapped
 *     output = this output buffer will be used unless too small
 * Returns: the case mapped string
 */
char[] toUpper(const(char)[] input, char[] output = null) {

    dchar[1] buf;
    // assume most common case: String stays the same length
    if (output.length < input.length)
        output.length = input.length;

    auto produced = 0;
    size_t ate;
    foreach(dchar ch; input) {
        // TODO Conditional Case Mapping
        UnicodeData *d = getUnicodeData(ch);
        if(d !is null && (d.generalCategory & UnicodeData.GeneralCategory.SpecialMapping)) {
            SpecialCaseData *s = getSpecialCaseData(ch);
            debug {
                assert(s !is null);
            }
            if(s.upperCaseMapping !is null) {
                // To speed up, use worst case for memory prealocation
                // since the length of an UpperCaseMapping list is at most 4
                // Make sure no relocation is made in the toString Method
                // better allocation algorithm ?
                if(produced + s.upperCaseMapping.length * 4 >= output.length)
                        output.length = output.length + output.length / 2 +  s.upperCaseMapping.length * 4;
                char[] res = toString(s.upperCaseMapping, output[produced..output.length], &ate);
                debug {
                    assert(ate == s.upperCaseMapping.length);
                    assert(res.ptr == output[produced..output.length].ptr);
                }
                produced += res.length;
                continue;
            }
        }
        // Make sure no relocation is made in the toString Method
        if(produced + 4 >= output.length)
            output.length = output.length + output.length / 2 + 4;
        buf[0] = d is null ? ch:d.simpleUpperCaseMapping;
        char[] res = toString(buf, output[produced..output.length], &ate);
        debug {
            assert(ate == 1);
            assert(res.ptr == output[produced..output.length].ptr);
        }
        produced += res.length;
    }
    return output[0..produced];
}


/**
 * Converts an Utf16 String to Upper case
 *
 * Params:
 *     input = String to be case mapped
 *     output = this output buffer will be used unless too small
 * Returns: the case mapped string
 */
wchar[] toUpper(const(wchar)[] input, wchar[] output = null) {

    dchar[1] buf;
    // assume most common case: String stays the same length
    if (output.length < input.length)
        output.length = input.length;

    auto produced = 0;
    size_t ate;
    foreach(dchar ch; input) {
        // TODO Conditional Case Mapping
        UnicodeData *d = getUnicodeData(ch);
        if(d !is null && (d.generalCategory & UnicodeData.GeneralCategory.SpecialMapping)) {
            SpecialCaseData *s = getSpecialCaseData(ch);
            debug {
                assert(s !is null);
            }
            if(s.upperCaseMapping !is null) {
                // To speed up, use worst case for memory prealocation
                // Make sure no relocation is made in the toString16 Method
                // better allocation algorithm ?
                if(produced + s.upperCaseMapping.length * 2 >= output.length)
                    output.length = output.length + output.length / 2 +  s.upperCaseMapping.length * 3;
                wchar[] res = toString16(s.upperCaseMapping, output[produced..output.length], &ate);
                debug {
                    assert(ate == s.upperCaseMapping.length);
                    assert(res.ptr == output[produced..output.length].ptr);
                }
                produced += res.length;
                continue;
            }
        }
        // Make sure no relocation is made in the toString16 Method
        if(produced + 4 >= output.length)
            output.length = output.length + output.length / 2 + 3;
        buf[0] = d is null ? ch:d.simpleUpperCaseMapping;
        wchar[] res = toString16(buf, output[produced..output.length], &ate);
        debug {
            assert(ate == 1);
            assert(res.ptr == output[produced..output.length].ptr);
        }
        produced += res.length;
    }
    return output[0..produced];
}

/**
 * Converts an Utf32 String to Upper case
 *
 * Params:
 *     input = String to be case mapped
 *     output = this output buffer will be used unless too small
 * Returns: the case mapped string
 */
dchar[] toUpper(const(dchar)[] input, dchar[] output = null) {

    // assume most common case: String stays the same length
    if (input.length > output.length)
        output.length = input.length;

    uint produced = 0;
    if (input.length)
        foreach(dchar orig; input) {
            // TODO Conditional Case Mapping
            UnicodeData *d = getUnicodeData(orig);
            if(d !is null && (d.generalCategory & UnicodeData.GeneralCategory.SpecialMapping)) {
                SpecialCaseData *s = getSpecialCaseData(orig);
                debug {
                    assert(s !is null);
                }
                if(s.upperCaseMapping !is null) {
                    // Better resize strategy ???
                    if(produced + s.upperCaseMapping.length  > output.length)
                        output.length = output.length + output.length / 2 + s.upperCaseMapping.length;
                    foreach(ch; s.upperCaseMapping) {
                        output[produced++] = ch;
                    }
                }
                continue;
            }
            if(produced >= output.length)
                output.length = output.length + output.length / 2;
            output[produced++] = d is null ? orig:d.simpleUpperCaseMapping;
        }
    return output[0..produced];
}


/**
 * Converts an Utf8 String to Lower case
 *
 * Params:
 *     input = String to be case mapped
 *     output = this output buffer will be used unless too small
 * Returns: the case mapped string
 */
char[] toLower(const(char)[] input, char[] output = null) {

    dchar[1] buf;
    // assume most common case: String stays the same length
    if (output.length < input.length)
        output.length = input.length;

    auto produced = 0;
    size_t ate;
    foreach(dchar ch; input) {
        // TODO Conditional Case Mapping
        UnicodeData *d = getUnicodeData(ch);
        if(d !is null && (d.generalCategory & UnicodeData.GeneralCategory.SpecialMapping)) {
            SpecialCaseData *s = getSpecialCaseData(ch);
            debug {
                assert(s !is null);
            }
            if(s.lowerCaseMapping !is null) {
                // To speed up, use worst case for memory prealocation
                // since the length of an LowerCaseMapping list is at most 4
                // Make sure no relocation is made in the toString Method
                // better allocation algorithm ?
                if(produced + s.lowerCaseMapping.length * 4 >= output.length)
                        output.length = output.length + output.length / 2 +  s.lowerCaseMapping.length * 4;
                char[] res = toString(s.lowerCaseMapping, output[produced..output.length], &ate);
                debug {
                    assert(ate == s.lowerCaseMapping.length);
                    assert(res.ptr == output[produced..output.length].ptr);
                }
                produced += res.length;
                continue;
            }
        }
        // Make sure no relocation is made in the toString Method
        if(produced + 4 >= output.length)
            output.length = output.length + output.length / 2 + 4;
        buf[0] = d is null ? ch:d.simpleLowerCaseMapping;
        char[] res = toString(buf, output[produced..output.length], &ate);
        debug {
            assert(ate == 1);
            assert(res.ptr == output[produced..output.length].ptr);
        }
        produced += res.length;
    }
    return output[0..produced];
}


/**
 * Converts an Utf16 String to Lower case
 *
 * Params:
 *     input = String to be case mapped
 *     output = this output buffer will be used unless too small
 * Returns: the case mapped string
 */
wchar[] toLower(const(wchar)[] input, wchar[] output = null) {

    dchar[1] buf;
    // assume most common case: String stays the same length
    if (output.length < input.length)
        output.length = input.length;

    auto produced = 0;
    size_t ate;
    foreach(dchar ch; input) {
        // TODO Conditional Case Mapping
        UnicodeData *d = getUnicodeData(ch);
        if(d !is null && (d.generalCategory & UnicodeData.GeneralCategory.SpecialMapping)) {
            SpecialCaseData *s = getSpecialCaseData(ch);
            debug {
                assert(s !is null);
            }
            if(s.lowerCaseMapping !is null) {
                // To speed up, use worst case for memory prealocation
                // Make sure no relocation is made in the toString16 Method
                // better allocation algorithm ?
                if(produced + s.lowerCaseMapping.length * 2 >= output.length)
                    output.length = output.length + output.length / 2 +  s.lowerCaseMapping.length * 3;
                wchar[] res = toString16(s.lowerCaseMapping, output[produced..output.length], &ate);
                debug {
                    assert(ate == s.lowerCaseMapping.length);
                    assert(res.ptr == output[produced..output.length].ptr);
                }
                produced += res.length;
                continue;
            }
        }
        // Make sure no relocation is made in the toString16 Method
        if(produced + 4 >= output.length)
            output.length = output.length + output.length / 2 + 3;
        buf[0] = d is null ? ch:d.simpleLowerCaseMapping;
        wchar[] res = toString16(buf, output[produced..output.length], &ate);
        debug {
            assert(ate == 1);
            assert(res.ptr == output[produced..output.length].ptr);
        }
        produced += res.length;
    }
    return output[0..produced];
}


/**
 * Converts an Utf32 String to Lower case
 *
 * Params:
 *     input = String to be case mapped
 *     output = this output buffer will be used unless too small
 * Returns: the case mapped string
 */
dchar[] toLower(const(dchar)[] input, dchar[] output = null) {

    // assume most common case: String stays the same length
    if (input.length > output.length)
        output.length = input.length;

    auto produced = 0;
    if (input.length)
        foreach(dchar orig; input) {
            // TODO Conditional Case Mapping
            UnicodeData *d = getUnicodeData(orig);
            if(d !is null && (d.generalCategory & UnicodeData.GeneralCategory.SpecialMapping)) {
                SpecialCaseData *s = getSpecialCaseData(orig);
                debug {
                    assert(s !is null);
                }
                if(s.lowerCaseMapping !is null) {
                    // Better resize strategy ???
                    if(produced + s.lowerCaseMapping.length  > output.length)
                        output.length = output.length + output.length / 2 + s.lowerCaseMapping.length;
                    foreach(ch; s.lowerCaseMapping) {
                        output[produced++] = ch;
                    }
                }
                continue;
            }
            if(produced >= output.length)
                output.length = output.length + output.length / 2;
            output[produced++] = d is null ? orig:d.simpleLowerCaseMapping;
        }
    return output[0..produced];
}

/**
 * Converts an Utf8 String to Folding case
 * Folding case is used for case insensitive comparsions.
 *
 * Params:
 *     input = String to be case mapped
 *     output = this output buffer will be used unless too small
 * Returns: the case mapped string
 */
char[] toFold(const(char)[] input, char[] output = null) {

    dchar[1] buf;
    // assume most common case: String stays the same length
    if (output.length < input.length)
        output.length = input.length;

    auto produced = 0;
    size_t ate;
    foreach(dchar ch; input) {
        FoldingCaseData *s = getFoldingCaseData(ch);
        if(s !is null) {
            // To speed up, use worst case for memory prealocation
            // since the length of an UpperCaseMapping list is at most 4
            // Make sure no relocation is made in the toString Method
            // better allocation algorithm ?
            if(produced + s.mapping.length * 4 >= output.length)
                output.length = output.length + output.length / 2 +  s.mapping.length * 4;
            char[] res = toString(s.mapping, output[produced..output.length], &ate);
            debug {
                assert(ate == s.mapping.length);
                assert(res.ptr == output[produced..output.length].ptr);
            }
            produced += res.length;
            continue;
        }
        // Make sure no relocation is made in the toString Method
        if(produced + 4 >= output.length)
            output.length = output.length + output.length / 2 + 4;
        buf[0] = ch;
        char[] res = toString(buf, output[produced..output.length], &ate);
        debug {
            assert(ate == 1);
            assert(res.ptr == output[produced..output.length].ptr);
        }
        produced += res.length;
    }
    return output[0..produced];
}

/**
 * Converts an Utf16 String to Folding case
 * Folding case is used for case insensitive comparsions.
 *
 * Params:
 *     input = String to be case mapped
 *     output = this output buffer will be used unless too small
 * Returns: the case mapped string
 */
wchar[] toFold(const(wchar)[] input, wchar[] output = null) {

    dchar[1] buf;
    // assume most common case: String stays the same length
    if (output.length < input.length)
        output.length = input.length;

    auto produced = 0;
    size_t ate;
    foreach(dchar ch; input) {
        FoldingCaseData *s = getFoldingCaseData(ch);
        if(s !is null) {
            // To speed up, use worst case for memory prealocation
            // Make sure no relocation is made in the toString16 Method
            // better allocation algorithm ?
            if(produced + s.mapping.length * 2 >= output.length)
                output.length = output.length + output.length / 2 +  s.mapping.length * 3;
            wchar[] res = toString16(s.mapping, output[produced..output.length], &ate);
            debug {
                assert(ate == s.mapping.length);
                assert(res.ptr == output[produced..output.length].ptr);
            }
            produced += res.length;
            continue;
        }
        // Make sure no relocation is made in the toString16 Method
        if(produced + 4 >= output.length)
            output.length = output.length + output.length / 2 + 3;
        buf[0] = ch;
        wchar[] res = toString16(buf, output[produced..output.length], &ate);
        debug {
            assert(ate == 1);
            assert(res.ptr == output[produced..output.length].ptr);
        }
        produced += res.length;
    }
    return output[0..produced];
}

/**
 * Converts an Utf32 String to Folding case
 * Folding case is used for case insensitive comparsions.
 *
 * Params:
 *     input = String to be case mapped
 *     output = this output buffer will be used unless too small
 * Returns: the case mapped string
 */
dchar[] toFold(const(dchar)[] input, dchar[] output = null) {

    // assume most common case: String stays the same length
    if (input.length > output.length)
        output.length = input.length;

    uint produced = 0;
    if (input.length)
        foreach(dchar orig; input) {
            FoldingCaseData *d = getFoldingCaseData(orig);
            if(d !is null ) {
                // Better resize strategy ???
                if(produced + d.mapping.length  > output.length)
                    output.length = output.length + output.length / 2 + d.mapping.length;
                foreach(ch; d.mapping) {
                    output[produced++] = ch;
                }
                continue;
            }
            if(produced >= output.length)
                output.length = output.length + output.length / 2;
            output[produced++] = orig;
        }
    return output[0..produced];
}


/**
 * Determines if a character is a digit. It returns true for decimal
 * digits only.
 *
 * Params:
 *     ch = the character to be inspected
 */
bool isDigit(dchar ch) {
    UnicodeData *d = getUnicodeData(ch);
    return (d !is null) && (d.generalCategory & UnicodeData.GeneralCategory.Nd);
}


/**
 * Determines if a character is a letter.
 *
 * Params:
 *     ch = the character to be inspected
 */
bool isLetter(int ch) {
    UnicodeData *d = getUnicodeData(ch);
    return (d !is null) && (d.generalCategory &
        ( UnicodeData.GeneralCategory.Lu
        | UnicodeData.GeneralCategory.Ll
        | UnicodeData.GeneralCategory.Lt
        | UnicodeData.GeneralCategory.Lm
        | UnicodeData.GeneralCategory.Lo));
}

/**
 * Determines if a character is a letter or a
 * decimal digit.
 *
 * Params:
 *     ch = the character to be inspected
 */
bool isLetterOrDigit(int ch) {
    UnicodeData *d = getUnicodeData(ch);
    return (d !is null) && (d.generalCategory &
        ( UnicodeData.GeneralCategory.Lu
        | UnicodeData.GeneralCategory.Ll
        | UnicodeData.GeneralCategory.Lt
        | UnicodeData.GeneralCategory.Lm
        | UnicodeData.GeneralCategory.Lo
        | UnicodeData.GeneralCategory.Nd));
}

/**
 * Determines if a character is a lower case letter.
 * Params:
 *     ch = the character to be inspected
 */
bool isLower(dchar ch) {
    UnicodeData *d = getUnicodeData(ch);
    return (d !is null) && (d.generalCategory & UnicodeData.GeneralCategory.Ll);
}

/**
 * Determines if a character is a title case letter.
 * In case of combined letters, only the first is upper and the second is lower.
 * Some of these special characters can be found in the croatian and greek language.
 * See_Also: http://en.wikipedia.org/wiki/Capitalization
 * Params:
 *     ch = the character to be inspected
 */
bool isTitle(dchar ch) {
    UnicodeData *d = getUnicodeData(ch);
    return (d !is null) && (d.generalCategory & UnicodeData.GeneralCategory.Lt);
}

/**
 * Determines if a character is a upper case letter.
 * Params:
 *     ch = the character to be inspected
 */
bool isUpper(dchar ch) {
    UnicodeData *d = getUnicodeData(ch);
    return (d !is null) && (d.generalCategory & UnicodeData.GeneralCategory.Lu);
}

/**
 * Determines if a character is a Whitespace character.
 * Whitespace characters are characters in the
 * General Catetories Zs, Zl, Zp without the No Break
 * spaces plus the control characters out of the ASCII
 * range, that are used as spaces:
 * TAB VT LF FF CR FS GS RS US NL
 *
 * WARNING: look at isSpace, maybe that function does
 *          more what you expect.
 *
 * Params:
 *     ch = the character to be inspected
 */
bool isWhitespace(dchar ch) {
    if((ch >= 0x0009 && ch <= 0x000D) || (ch >= 0x001C && ch <= 0x001F))
        return true;
    UnicodeData *d = getUnicodeData(ch);
    return (d !is null) && (d.generalCategory &
            ( UnicodeData.GeneralCategory.Zs
            | UnicodeData.GeneralCategory.Zl
            | UnicodeData.GeneralCategory.Zp))
            && ch != 0x00A0 // NBSP
            && ch != 0x202F // NARROW NBSP
            && ch != 0xFEFF; // ZERO WIDTH NBSP
}

/**
 * Detemines if a character is a Space character as
 * specified in the Unicode Standard.
 *
 * WARNING: look at isWhitespace, maybe that function does
 *          more what you expect.
 *
 * Params:
 *     ch = the character to be inspected
 */
bool isSpace(dchar ch) {
    UnicodeData *d = getUnicodeData(ch);
    return (d !is null) && (d.generalCategory &
            ( UnicodeData.GeneralCategory.Zs
            | UnicodeData.GeneralCategory.Zl
            | UnicodeData.GeneralCategory.Zp));
}


/**
 * Detemines if a character is a printable character as
 * specified in the Unicode Standard.
 *
 * Params:
 *     ch = the character to be inspected
 */
bool isPrintable(dchar ch) {
    UnicodeData *d = getUnicodeData(ch);
    return (d !is null) && !(d.generalCategory &
            ( UnicodeData.GeneralCategory.Cn
            | UnicodeData.GeneralCategory.Cc
            | UnicodeData.GeneralCategory.Cf
            | UnicodeData.GeneralCategory.Co
            | UnicodeData.GeneralCategory.Cs));
}

debug ( UnicodeTest ):
    void main() {}

debug (UnitTest) {

unittest {


    // 1) No Buffer passed, no resize, no SpecialCase

    const(char)[] testString1utf8 = "\u00E4\u00F6\u00FC";
    const(wchar)[] testString1utf16 = "\u00E4\u00F6\u00FC";
    const(dchar)[] testString1utf32 = "\u00E4\u00F6\u00FC";
    const(char)[] refString1utf8 = "\u00C4\u00D6\u00DC";
    const(wchar)[] refString1utf16 = "\u00C4\u00D6\u00DC";
    const(dchar)[] refString1utf32 = "\u00C4\u00D6\u00DC";
    char[] resultString1utf8 = toUpper(testString1utf8);
    assert(resultString1utf8 == refString1utf8);
    wchar[] resultString1utf16 = toUpper(testString1utf16);
    assert(resultString1utf16 == refString1utf16);
    dchar[] resultString1utf32 = toUpper(testString1utf32);
    assert(resultString1utf32 == refString1utf32);

    // 2) Buffer passed, no resize, no SpecialCase
    char[60] buffer1utf8;
    wchar[30] buffer1utf16;
    dchar[30] buffer1utf32;
    resultString1utf8 = toUpper(testString1utf8,buffer1utf8);
    assert(resultString1utf8.ptr == buffer1utf8.ptr);
    assert(resultString1utf8 == refString1utf8);
    resultString1utf16 = toUpper(testString1utf16,buffer1utf16);
    assert(resultString1utf16.ptr == buffer1utf16.ptr);
    assert(resultString1utf16 == refString1utf16);
    resultString1utf32 = toUpper(testString1utf32,buffer1utf32);
    assert(resultString1utf32.ptr == buffer1utf32.ptr);
    assert(resultString1utf32 == refString1utf32);

    // 3/ Buffer passed, resize necessary, no Special case

    char[5] buffer2utf8;
    wchar[2] buffer2utf16;
    dchar[2] buffer2utf32;
    resultString1utf8 = toUpper(testString1utf8,buffer2utf8);
    assert(resultString1utf8.ptr != buffer2utf8.ptr);
    assert(resultString1utf8 == refString1utf8);
    resultString1utf16 = toUpper(testString1utf16,buffer2utf16);
    assert(resultString1utf16.ptr != buffer2utf16.ptr);
    assert(resultString1utf16 == refString1utf16);
    resultString1utf32 = toUpper(testString1utf32,buffer2utf32);
    assert(resultString1utf32.ptr != buffer2utf32.ptr);
    assert(resultString1utf32 == refString1utf32);

    // 4) Buffer passed, resize necessary, extensive SpecialCase


    const(char)[] testString2utf8 = "\uFB03\uFB04\uFB05";
    const(wchar)[] testString2utf16 = "\uFB03\uFB04\uFB05";
    const(dchar)[] testString2utf32 = "\uFB03\uFB04\uFB05";
    const(char)[] refString2utf8 = "\u0046\u0046\u0049\u0046\u0046\u004C\u0053\u0054";
    const(wchar)[] refString2utf16 = "\u0046\u0046\u0049\u0046\u0046\u004C\u0053\u0054";
    const(dchar)[] refString2utf32 = "\u0046\u0046\u0049\u0046\u0046\u004C\u0053\u0054";
    resultString1utf8 = toUpper(testString2utf8,buffer2utf8);
    assert(resultString1utf8.ptr != buffer2utf8.ptr);
    assert(resultString1utf8 == refString2utf8);
    resultString1utf16 = toUpper(testString2utf16,buffer2utf16);
    assert(resultString1utf16.ptr != buffer2utf16.ptr);
    assert(resultString1utf16 == refString2utf16);
    resultString1utf32 = toUpper(testString2utf32,buffer2utf32);
    assert(resultString1utf32.ptr != buffer2utf32.ptr);
    assert(resultString1utf32 == refString2utf32);

}


unittest {


    // 1) No Buffer passed, no resize, no SpecialCase

    const(char)[] testString1utf8 = "\u00C4\u00D6\u00DC";
    const(wchar)[] testString1utf16 = "\u00C4\u00D6\u00DC";
    const(dchar)[] testString1utf32 = "\u00C4\u00D6\u00DC";
    const(char)[] refString1utf8 = "\u00E4\u00F6\u00FC";
    const(wchar)[] refString1utf16 = "\u00E4\u00F6\u00FC";
    const(dchar)[] refString1utf32 = "\u00E4\u00F6\u00FC";
    const(char)[] resultString1utf8 = toLower(testString1utf8);
    assert(resultString1utf8 == refString1utf8);
    const(wchar)[] resultString1utf16 = toLower(testString1utf16);
    assert(resultString1utf16 == refString1utf16);
    const(dchar)[] resultString1utf32 = toLower(testString1utf32);
    assert(resultString1utf32 == refString1utf32);

    // 2) Buffer passed, no resize, no SpecialCase
    char[60] buffer1utf8;
    wchar[30] buffer1utf16;
    dchar[30] buffer1utf32;
    resultString1utf8 = toLower(testString1utf8,buffer1utf8);
    assert(resultString1utf8.ptr == buffer1utf8.ptr);
    assert(resultString1utf8 == refString1utf8);
    resultString1utf16 = toLower(testString1utf16,buffer1utf16);
    assert(resultString1utf16.ptr == buffer1utf16.ptr);
    assert(resultString1utf16 == refString1utf16);
    resultString1utf32 = toLower(testString1utf32,buffer1utf32);
    assert(resultString1utf32.ptr == buffer1utf32.ptr);
    assert(resultString1utf32 == refString1utf32);

    // 3/ Buffer passed, resize necessary, no Special case

    char[5] buffer2utf8;
    wchar[2] buffer2utf16;
    dchar[2] buffer2utf32;
    resultString1utf8 = toLower(testString1utf8,buffer2utf8);
    assert(resultString1utf8.ptr != buffer2utf8.ptr);
    assert(resultString1utf8 == refString1utf8);
    resultString1utf16 = toLower(testString1utf16,buffer2utf16);
    assert(resultString1utf16.ptr != buffer2utf16.ptr);
    assert(resultString1utf16 == refString1utf16);
    resultString1utf32 = toLower(testString1utf32,buffer2utf32);
    assert(resultString1utf32.ptr != buffer2utf32.ptr);
    assert(resultString1utf32 == refString1utf32);

    // 4) Buffer passed, resize necessary, extensive SpecialCase

    const(char)[] testString2utf8 = "\u0130\u0130\u0130";
    const(wchar)[] testString2utf16 = "\u0130\u0130\u0130";
    const(dchar)[] testString2utf32 = "\u0130\u0130\u0130";
    const(char)[] refString2utf8 = "\u0069\u0307\u0069\u0307\u0069\u0307";
    const(wchar)[] refString2utf16 = "\u0069\u0307\u0069\u0307\u0069\u0307";
    const(dchar)[] refString2utf32 = "\u0069\u0307\u0069\u0307\u0069\u0307";
    resultString1utf8 = toLower(testString2utf8,buffer2utf8);
    assert(resultString1utf8.ptr != buffer2utf8.ptr);
    assert(resultString1utf8 == refString2utf8);
    resultString1utf16 = toLower(testString2utf16,buffer2utf16);
    assert(resultString1utf16.ptr != buffer2utf16.ptr);
    assert(resultString1utf16 == refString2utf16);
    resultString1utf32 = toLower(testString2utf32,buffer2utf32);
    assert(resultString1utf32.ptr != buffer2utf32.ptr);
    assert(resultString1utf32 == refString2utf32);
}

unittest {
    const(char)[] testString1utf8 = "?!Mädchen \u0390\u0390,;";
    const(char)[] testString2utf8 = "?!MÄDCHEN \u03B9\u0308\u0301\u03B9\u0308\u0301,;";
    assert(toFold(testString1utf8) == toFold(testString2utf8));
    const(wchar)[] testString1utf16 = "?!Mädchen \u0390\u0390,;";
    const(wchar)[] testString2utf16 = "?!MÄDCHEN \u03B9\u0308\u0301\u03B9\u0308\u0301,;";
    assert(toFold(testString1utf16) == toFold(testString2utf16));
    const(wchar)[] testString1utf32 = "?!Mädchen \u0390\u0390,;";
    const(wchar)[] testString2utf32 = "?!MÄDCHEN \u03B9\u0308\u0301\u03B9\u0308\u0301,;";
    assert(toFold(testString1utf32) == toFold(testString2utf32));
}

}