Exemplo n.º 1
0
        private unsafe static int FindStringOrdinal(char *source, int sourceCount, char *value,int valueCount, FindStringOptions option, bool ignoreCase)
        {
            int ctrSource = 0;  // index value into source
            int ctrValue = 0;   // index value into value
            char sourceChar;    // Character for case lookup in source
            char valueChar;     // Character for case lookup in value
            int lastSourceStart;

            Contract.Assert(source != null);
            Contract.Assert(value != null);
            Contract.Assert(sourceCount>= 0);
            Contract.Assert(valueCount >= 0);

            if(valueCount == 0)
            {
                switch (option)
                {
                    case FindStringOptions.StartsWith:
                    case FindStringOptions.Start:
                        return(0);

                    case FindStringOptions.EndsWith:
                    case FindStringOptions.End:
                        return(sourceCount);

                    default:
                        return -1;
                }
            }

            if(sourceCount < valueCount)
            {
                return -1;
            }

            switch (option)
            {
                case FindStringOptions.StartsWith:
                {
                    if (ignoreCase)
                    {
                        for (ctrValue = 0; ctrValue < valueCount; ctrValue++)
                        {
                            sourceChar = ToUpper(source[ctrValue]);
                            valueChar  = ToUpper(value[ctrValue]);

                            if (sourceChar != valueChar)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        for (ctrValue = 0; ctrValue < valueCount; ctrValue++)
                        {
                                sourceChar = source[ctrValue];
                                valueChar  = value[ctrValue];

                                if (sourceChar != valueChar)
                                {
                                    break;
                                }
                        }
                    }

                    if (ctrValue == valueCount)
                    {
                        return 0;
                    }
                }
                break;

                case FindStringOptions.Start:
                {
                    lastSourceStart = sourceCount - valueCount;
                    if (ignoreCase)
                    {
                        char firstValueChar = ToUpper(value[0]);
                        for (ctrSource = 0; ctrSource <= lastSourceStart; ctrSource++)
                        {
                            sourceChar = ToUpper(source[ctrSource]);
                            if (sourceChar != firstValueChar)
                            {
                                continue;
                            }

                            for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
                            {
                                sourceChar = ToUpper(source[ctrSource + ctrValue]);
                                valueChar  = ToUpper(value[ctrValue]);

                                if (sourceChar != valueChar)
                                {
                                    break;
                                }
                            }

                            if (ctrValue == valueCount)
                            {
                                return ctrSource;
                            }
                        }
                    }
                    else
                    {
                        char firstValueChar = value[0];
                        for (ctrSource = 0; ctrSource <= lastSourceStart; ctrSource++)
                        {
                            sourceChar = source[ctrSource];
                            if (sourceChar != firstValueChar)
                            {
                                continue;
                            }

                            for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
                            {
                                sourceChar = source[ctrSource + ctrValue];
                                valueChar  = value[ctrValue];

                                if (sourceChar != valueChar)
                                {
                                    break;
                                }
                            }

                            if (ctrValue == valueCount)
                            {
                                return ctrSource;
                            }
                        }
                    }
                }
                break;

                case FindStringOptions.EndsWith:
                {
                    lastSourceStart = sourceCount - valueCount;
                    if (ignoreCase)
                    {
                        for (ctrSource = lastSourceStart, ctrValue = 0; ctrValue < valueCount; ctrSource++,ctrValue++)
                        {
                            sourceChar = ToUpper(source[ctrSource]);
                            valueChar  = ToUpper(value[ctrValue]);

                            if (sourceChar != valueChar)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        for (ctrSource = lastSourceStart, ctrValue = 0; ctrValue < valueCount; ctrSource++,ctrValue++)
                        {
                            sourceChar = source[ctrSource];
                            valueChar  = value[ctrValue];

                            if (sourceChar != valueChar)
                            {
                                break;
                            }
                        }
                    }

                    if (ctrValue == valueCount)
                    {
                        return sourceCount - valueCount;
                    }
                }
                break;

                case FindStringOptions.End:
                {
                    lastSourceStart = sourceCount - valueCount;
                    if (ignoreCase)
                    {
                        char firstValueChar = ToUpper(value[0]);
                        for (ctrSource = lastSourceStart; ctrSource >= 0; ctrSource--)
                        {
                            sourceChar = ToUpper(source[ctrSource]);
                            if(sourceChar != firstValueChar)
                            {
                                continue;
                            }
                            for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
                            {
                                sourceChar = ToUpper(source[ctrSource + ctrValue]);
                                valueChar  = ToUpper(value[ctrValue]);

                                if (sourceChar != valueChar)
                                {
                                    break;
                                }
                            }

                            if (ctrValue == valueCount)
                            {
                                return ctrSource;
                            }
                        }
                    } else {
                        char firstValueChar = value[0];
                        for (ctrSource = lastSourceStart; ctrSource >= 0; ctrSource--)
                        {
                            sourceChar = source[ctrSource];
                            if(sourceChar != firstValueChar)
                            {
                                continue;
                            }

                            for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
                            {
                                sourceChar = source[ctrSource + ctrValue];
                                valueChar  = value[ctrValue];

                                if (sourceChar != valueChar)
                                {
                                    break;
                                }
                            }

                            if (ctrValue == valueCount)
                            {
                                return ctrSource;
                            }
                        }
                    }
                }
                break;

                default:
                    return -1;
            }

            return -1;
        }
Exemplo n.º 2
0
        private unsafe static int FindStringOrdinal(char *source, int sourceCount, char *value, int valueCount, FindStringOptions option, bool ignoreCase)
        {
            int  ctrSource = 0; // index value into source
            int  ctrValue  = 0; // index value into value
            char sourceChar;    // Character for case lookup in source
            char valueChar;     // Character for case lookup in value
            int  lastSourceStart;

            Debug.Assert(source != null);
            Debug.Assert(value != null);
            Debug.Assert(sourceCount >= 0);
            Debug.Assert(valueCount >= 0);

            if (valueCount == 0)
            {
                switch (option)
                {
                case FindStringOptions.StartsWith:
                case FindStringOptions.Start:
                    return(0);

                case FindStringOptions.EndsWith:
                case FindStringOptions.End:
                    return(sourceCount);

                default:
                    return(-1);
                }
            }

            if (sourceCount < valueCount)
            {
                return(-1);
            }

            switch (option)
            {
            case FindStringOptions.StartsWith:
            {
                if (ignoreCase)
                {
                    for (ctrValue = 0; ctrValue < valueCount; ctrValue++)
                    {
                        sourceChar = ToUpper(source[ctrValue]);
                        valueChar  = ToUpper(value[ctrValue]);

                        if (sourceChar != valueChar)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    for (ctrValue = 0; ctrValue < valueCount; ctrValue++)
                    {
                        sourceChar = source[ctrValue];
                        valueChar  = value[ctrValue];

                        if (sourceChar != valueChar)
                        {
                            break;
                        }
                    }
                }

                if (ctrValue == valueCount)
                {
                    return(0);
                }
            }
            break;

            case FindStringOptions.Start:
            {
                lastSourceStart = sourceCount - valueCount;
                if (ignoreCase)
                {
                    char firstValueChar = ToUpper(value[0]);
                    for (ctrSource = 0; ctrSource <= lastSourceStart; ctrSource++)
                    {
                        sourceChar = ToUpper(source[ctrSource]);
                        if (sourceChar != firstValueChar)
                        {
                            continue;
                        }

                        for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
                        {
                            sourceChar = ToUpper(source[ctrSource + ctrValue]);
                            valueChar  = ToUpper(value[ctrValue]);

                            if (sourceChar != valueChar)
                            {
                                break;
                            }
                        }

                        if (ctrValue == valueCount)
                        {
                            return(ctrSource);
                        }
                    }
                }
                else
                {
                    char firstValueChar = value[0];
                    for (ctrSource = 0; ctrSource <= lastSourceStart; ctrSource++)
                    {
                        sourceChar = source[ctrSource];
                        if (sourceChar != firstValueChar)
                        {
                            continue;
                        }

                        for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
                        {
                            sourceChar = source[ctrSource + ctrValue];
                            valueChar  = value[ctrValue];

                            if (sourceChar != valueChar)
                            {
                                break;
                            }
                        }

                        if (ctrValue == valueCount)
                        {
                            return(ctrSource);
                        }
                    }
                }
            }
            break;

            case FindStringOptions.EndsWith:
            {
                lastSourceStart = sourceCount - valueCount;
                if (ignoreCase)
                {
                    for (ctrSource = lastSourceStart, ctrValue = 0; ctrValue < valueCount; ctrSource++, ctrValue++)
                    {
                        sourceChar = ToUpper(source[ctrSource]);
                        valueChar  = ToUpper(value[ctrValue]);

                        if (sourceChar != valueChar)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    for (ctrSource = lastSourceStart, ctrValue = 0; ctrValue < valueCount; ctrSource++, ctrValue++)
                    {
                        sourceChar = source[ctrSource];
                        valueChar  = value[ctrValue];

                        if (sourceChar != valueChar)
                        {
                            break;
                        }
                    }
                }

                if (ctrValue == valueCount)
                {
                    return(sourceCount - valueCount);
                }
            }
            break;

            case FindStringOptions.End:
            {
                lastSourceStart = sourceCount - valueCount;
                if (ignoreCase)
                {
                    char firstValueChar = ToUpper(value[0]);
                    for (ctrSource = lastSourceStart; ctrSource >= 0; ctrSource--)
                    {
                        sourceChar = ToUpper(source[ctrSource]);
                        if (sourceChar != firstValueChar)
                        {
                            continue;
                        }
                        for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
                        {
                            sourceChar = ToUpper(source[ctrSource + ctrValue]);
                            valueChar  = ToUpper(value[ctrValue]);

                            if (sourceChar != valueChar)
                            {
                                break;
                            }
                        }

                        if (ctrValue == valueCount)
                        {
                            return(ctrSource);
                        }
                    }
                }
                else
                {
                    char firstValueChar = value[0];
                    for (ctrSource = lastSourceStart; ctrSource >= 0; ctrSource--)
                    {
                        sourceChar = source[ctrSource];
                        if (sourceChar != firstValueChar)
                        {
                            continue;
                        }

                        for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
                        {
                            sourceChar = source[ctrSource + ctrValue];
                            valueChar  = value[ctrValue];

                            if (sourceChar != valueChar)
                            {
                                break;
                            }
                        }

                        if (ctrValue == valueCount)
                        {
                            return(ctrSource);
                        }
                    }
                }
            }
            break;

            default:
                return(-1);
            }

            return(-1);
        }