summaryrefslogtreecommitdiffstats
path: root/extensions/spellcheck/src/mozEnglishWordUtils.cpp
blob: f3ae8a0a73c8f03452dcd8e82ffffeeaa1faf765 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozEnglishWordUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsReadableUtils.h"
#include "nsUnicharUtils.h"
#include "nsUnicodeProperties.h"
#include "nsCRT.h"
#include "mozilla/Likely.h"

NS_IMPL_CYCLE_COLLECTION(mozEnglishWordUtils, mURLDetector)

mozEnglishWordUtils::mozEnglishWordUtils() {
  mURLDetector = do_CreateInstance(MOZ_TXTTOHTMLCONV_CONTRACTID);
}

mozEnglishWordUtils::~mozEnglishWordUtils() {}

// This needs vast improvement

// static
bool mozEnglishWordUtils::ucIsAlpha(char16_t aChar) {
  // XXX we have to fix callers to handle the full Unicode range
  return nsUGenCategory::kLetter == mozilla::unicode::GetGenCategory(aChar);
}

bool mozEnglishWordUtils::FindNextWord(const nsAString& aWord, uint32_t offset,
                                       int32_t* begin, int32_t* end) {
  if (offset >= aWord.Length()) {
    *begin = -1;
    *end = -1;
    return false;
  }

  const char16_t* word = aWord.BeginReading();
  uint32_t length = aWord.Length();
  const char16_t* p = word + offset;
  const char16_t* endbuf = word + length;
  const char16_t* startWord = p;

  // XXX These loops should be modified to handle non-BMP characters.
  // if previous character is a word character, need to advance out of the
  // word
  if (offset > 0 && ucIsAlpha(*(p - 1))) {
    while (p < endbuf && ucIsAlpha(*p)) {
      p++;
    }
  }
  while ((p < endbuf) && (!ucIsAlpha(*p))) {
    p++;
  }
  startWord = p;
  while ((p < endbuf) && ((ucIsAlpha(*p)) || (*p == '\''))) {
    p++;
  }

  // we could be trying to break down a url, we don't want to break a url into
  // parts, instead we want to find out if it really is a url and if so, skip
  // it, advancing startWord to a point after the url.

  // before we spend more time looking to see if the word is a url, look for a
  // url identifer and make sure that identifer isn't the last character in
  // the word fragment.
  if ((p < endbuf - 1) && (*p == ':' || *p == '@' || *p == '.')) {
    // ok, we have a possible url...do more research to find out if we really
    // have one and determine the length of the url so we can skip over it.

    if (mURLDetector) {
      int32_t startPos = -1;
      int32_t endPos = -1;

      mURLDetector->FindURLInPlaintext(startWord, endbuf - startWord,
                                       p - startWord, &startPos, &endPos);

      // ok, if we got a url, adjust the array bounds, skip the current url
      // text and find the next word again
      if (startPos != -1 && endPos != -1) {
        startWord = p + endPos + 1;  // skip over the url

        // now recursively call FindNextWord to search for the next word now
        // that we have skipped the url
        return FindNextWord(aWord, startWord - word, begin, end);
      }
    }
  }

  while ((p > startWord) && (*(p - 1) == '\'')) {  // trim trailing apostrophes
    p--;
  }

  if (startWord == endbuf) {
    *begin = -1;
    *end = -1;
    return false;
  }
  *begin = startWord - word;
  *end = p - word;
  return true;
}