/// <summary>
        /// Initialises or returns unmanaged pointer to BreakIterator
        /// </summary>
        /// <returns></returns>
        private void InitializeBreakIterator()
        {
            if (_breakIterator != IntPtr.Zero)
            {
                return;
            }

            if (_rules != null)
            {
                ErrorCode  errorCode;
                ParseError parseError;

                _breakIterator = NativeMethods.ubrk_openRules(_rules, _rules.Length, Text, Text.Length, out parseError, out errorCode);

                if (errorCode.IsFailure())
                {
                    throw new ParseErrorException("Couldn't create RuleBasedBreakIterator with the given rules!", parseError, _rules);
                }
            }
            else
            {
                ErrorCode errorCode;
                _breakIterator = NativeMethods.ubrk_open(_iteratorType, _locale.Id, Text, Text.Length, out errorCode);
                if (errorCode.IsFailure())
                {
                    throw new InvalidOperationException(
                              string.Format("Could not create a BreakIterator with locale [{0}], BreakIteratorType [{1}] and Text [{2}]", _locale.Id, _iteratorType, Text));
                }
            }
        }
Esempio n. 2
0
        public static IEnumerable <string> Split(UBreakIteratorType type, string locale, string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(new string[] { });
            }


            ErrorCode err;
            IntPtr    bi = NativeMethods.ubrk_open(type, locale, text, text.Length, out err);

            if (err != ErrorCode.NoErrors)
            {
                throw new Exception("BreakIterator.Split() failed with code " + err);
            }
            var tokens = new List <string>();
            int cur    = NativeMethods.ubrk_first(bi);

            while (cur != DONE)
            {
                int next   = NativeMethods.ubrk_next(bi);
                int status = NativeMethods.ubrk_getRuleStatus(bi);
                if (next != DONE && AddToken(type, status))
                {
                    tokens.Add(text.Substring(cur, next - cur));
                }
                cur = next;
            }
            NativeMethods.ubrk_close(bi);
            return(tokens);
        }