Flutter Engine
The Flutter Engine
string_range_sanitization.mm
Go to the documentation of this file.
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 }
16 return NSMakeRange(index, 0);
17}
18
19NSRange RangeForCharactersInRange(NSString* text, NSRange range) {
20 if (text == nil || range.location + range.length > text.length) {
21 return NSMakeRange(NSNotFound, 0);
22 }
23 NSRange sanitizedRange = [text rangeOfComposedCharacterSequencesForRange:range];
24 // We don't want to override the length, we just want to make sure we don't
25 // select into the middle of a multi-byte character. Taking the
26 // `sanitizedRange`'s length will end up altering the actual selection.
27 return NSMakeRange(sanitizedRange.location, range.length);
28}
29
30} // namespace fml
std::u16string text
Definition: ascii_trie.cc:9
NSRange RangeForCharacterAtIndex(NSString *text, NSUInteger index)
NSRange RangeForCharactersInRange(NSString *text, NSRange range)