• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/fml/platform/darwin/string_range_sanitization.h"
6
7namespace fml {
8
9NSRange RangeForCharacterAtIndex(NSString* text, NSUInteger index) {
10  if (text == nil || index >= text.length) {
11    return NSMakeRange(NSNotFound, 0);
12  }
13  if (index < text.length)
14    return [text rangeOfComposedCharacterSequenceAtIndex:index];
15  return NSMakeRange(index, 0);
16}
17
18NSRange RangeForCharactersInRange(NSString* text, NSRange range) {
19  if (text == nil || range.location + range.length > text.length) {
20    return NSMakeRange(NSNotFound, 0);
21  }
22  NSRange sanitizedRange = [text rangeOfComposedCharacterSequencesForRange:range];
23  // We don't want to override the length, we just want to make sure we don't
24  // select into the middle of a multi-byte character. Taking the
25  // `sanitizedRange`'s length will end up altering the actual selection.
26  return NSMakeRange(sanitizedRange.location, range.length);
27}
28
29}  // namespace fml
30