Example #1
0
        public static object ReverseIndex(MutableString/*!*/ self,
            int character, [DefaultProtocol, DefaultParameterValue(-1)]int start) {

            start = NormalizeIndex(self, start);
            if (start < 0 || character < 0 || character > 255) {
                return null;
            }

            if (start >= self.Length) {
                start = self.Length - 1;
            }
            
            int result = self.LastIndexOf((byte)character, start);
            return (result != -1) ? ScriptingRuntimeHelpers.Int32ToObject(result) : null;
        }
Example #2
0
        public static object LastIndexOf(MutableString/*!*/ self,
            int character, [DefaultProtocol, DefaultParameterValue(Int32.MaxValue)]int start) {

            if (!self.IsBinaryEncoded && !self.Encoding.IsKCoding && !self.IsAscii()) {
                throw RubyExceptions.CreateTypeError("type mismatch: Fixnum given");
            }

            int byteCount = self.GetByteCount();
            start = IListOps.NormalizeIndex(byteCount, start);
            if (start < 0 || character < 0 || character > 255) {
                return null;
            }

            if (start >= byteCount) {
                start = byteCount - 1;
            }
            
            int result = self.LastIndexOf((byte)character, start);
            return (result != -1) ? ScriptingRuntimeHelpers.Int32ToObject(result) : null;
        }
Example #3
0
        public static object ReverseIndex(MutableString/*!*/ self,
            [DefaultProtocol, NotNull]MutableString/*!*/ substring, [DefaultProtocol]int start) {
            start = NormalizeIndex(self, start);

            if (start < 0) {
                return null;
            }

            if (substring.IsEmpty) {
                return ScriptingRuntimeHelpers.Int32ToObject((start >= self.Length) ? self.Length : start);
            }

            start += substring.Length - 1;

            if (start >= self.Length) {
                start = self.Length - 1;
            }

            int result = self.LastIndexOf(substring, start);
            return (result != -1) ? ScriptingRuntimeHelpers.Int32ToObject(result) : null;
        }
Example #4
0
        public static object LastIndexOf(MutableString/*!*/ self,
            [DefaultProtocol, NotNull]MutableString/*!*/ substring, [DefaultProtocol, DefaultParameterValue(Int32.MaxValue)]int start) {

            self.SwitchToCharacters();
            int charCount = self.GetCharCount();

            start = IListOps.NormalizeIndex(charCount, start);
            if (start < 0) {
                return null;
            }

            if (substring.IsEmpty) {
                return ScriptingRuntimeHelpers.Int32ToObject((start >= charCount) ? charCount : start);
            }

            self.RequireCompatibleEncoding(substring);
            substring.SwitchToCharacters();
            int subCharCount = substring.GetCharCount();

            // LastIndexOf has CLR semantics: no characters of the substring are matched beyond start position.
            // Hence we need to increase start by the length of the substring - 1.
            if (start > charCount - subCharCount) {
                start = charCount - 1;
            } else {
                start += subCharCount - 1;
            }

            int result = self.LastIndexOf(substring, start);
            return (result != -1) ? ScriptingRuntimeHelpers.Int32ToObject(result) : null;
        }