summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/editing/state_machines/backward_code_point_state_machine.cc
blob: a63484d578712a33edbbe741517111321a26c898 (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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/editing/state_machines/backward_code_point_state_machine.h"

#include "base/notreached.h"

namespace blink {

enum class BackwardCodePointStateMachine::BackwardCodePointState {
  kNotSurrogate,
  kTrailSurrogate,
  kInvalid,
};

BackwardCodePointStateMachine::BackwardCodePointStateMachine()
    : state_(BackwardCodePointState::kNotSurrogate) {}

TextSegmentationMachineState
BackwardCodePointStateMachine::FeedPrecedingCodeUnit(UChar code_unit) {
  switch (state_) {
    case BackwardCodePointState::kNotSurrogate:
      if (U16_IS_LEAD(code_unit)) {
        code_units_to_be_deleted_ = 0;
        state_ = BackwardCodePointState::kInvalid;
        return TextSegmentationMachineState::kInvalid;
      }
      ++code_units_to_be_deleted_;
      if (U16_IS_TRAIL(code_unit)) {
        state_ = BackwardCodePointState::kTrailSurrogate;
        return TextSegmentationMachineState::kNeedMoreCodeUnit;
      }
      return TextSegmentationMachineState::kFinished;
    case BackwardCodePointState::kTrailSurrogate:
      if (U16_IS_LEAD(code_unit)) {
        ++code_units_to_be_deleted_;
        state_ = BackwardCodePointState::kNotSurrogate;
        return TextSegmentationMachineState::kFinished;
      }
      code_units_to_be_deleted_ = 0;
      state_ = BackwardCodePointState::kInvalid;
      return TextSegmentationMachineState::kInvalid;
    case BackwardCodePointState::kInvalid:
      code_units_to_be_deleted_ = 0;
      return TextSegmentationMachineState::kInvalid;
  }
  NOTREACHED();
  return TextSegmentationMachineState::kInvalid;
}

TextSegmentationMachineState
BackwardCodePointStateMachine::FeedFollowingCodeUnit(UChar code_unit) {
  NOTREACHED();
  return TextSegmentationMachineState::kInvalid;
}

bool BackwardCodePointStateMachine::AtCodePointBoundary() {
  return state_ == BackwardCodePointState::kNotSurrogate;
}

int BackwardCodePointStateMachine::GetBoundaryOffset() {
  return -code_units_to_be_deleted_;
}

void BackwardCodePointStateMachine::Reset() {
  code_units_to_be_deleted_ = 0;
  state_ = BackwardCodePointState::kNotSurrogate;
}

}  // namespace blink