Home | History | Annotate | Download | only in range
      1 // Copyright (c) 2011 The Chromium 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 "ui/gfx/range/range.h"
      6 
      7 #include <stddef.h>
      8 
      9 #include <limits>
     10 
     11 #include "base/logging.h"
     12 
     13 namespace gfx {
     14 
     15 Range::Range(const NSRange& range) {
     16   *this = range;
     17 }
     18 
     19 Range& Range::operator=(const NSRange& range) {
     20   if (range.location == NSNotFound) {
     21     DCHECK_EQ(0U, range.length);
     22     *this = InvalidRange();
     23   } else {
     24     set_start(range.location);
     25     // Don't overflow |end_|.
     26     DCHECK_LE(range.length, std::numeric_limits<size_t>::max() - start());
     27     set_end(start() + range.length);
     28   }
     29   return *this;
     30 }
     31 
     32 NSRange Range::ToNSRange() const {
     33   if (!IsValid())
     34     return NSMakeRange(NSNotFound, 0);
     35   return NSMakeRange(GetMin(), length());
     36 }
     37 
     38 }  // namespace gfx
     39