| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013, Google Inc. All rights reserved. | 2 * Copyright (C) 2013, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 */ | 23 */ |
| 24 | 24 |
| 25 #include "wtf/text/TextPosition.h" | 25 #include "wtf/text/TextPosition.h" |
| 26 | 26 |
| 27 #include "wtf/PassOwnPtr.h" | 27 #include "wtf/PassOwnPtr.h" |
| 28 #include "wtf/StdLibExtras.h" | 28 #include "wtf/StdLibExtras.h" |
| 29 #include <algorithm> | 29 #include <algorithm> |
| 30 | 30 |
| 31 namespace WTF { | 31 namespace WTF { |
| 32 | 32 |
| 33 PassOwnPtr<Vector<unsigned>> lineEndings(const String& text) | 33 PassOwnPtr<Vector<unsigned>> lineEndings(const String& text) { |
| 34 { | 34 OwnPtr<Vector<unsigned>> result(adoptPtr(new Vector<unsigned>())); |
| 35 OwnPtr<Vector<unsigned>> result(adoptPtr(new Vector<unsigned>())); | |
| 36 | 35 |
| 37 unsigned start = 0; | 36 unsigned start = 0; |
| 38 while (start < text.length()) { | 37 while (start < text.length()) { |
| 39 size_t lineEnd = text.find('\n', start); | 38 size_t lineEnd = text.find('\n', start); |
| 40 if (lineEnd == kNotFound) | 39 if (lineEnd == kNotFound) |
| 41 break; | 40 break; |
| 42 | 41 |
| 43 result->append(static_cast<unsigned>(lineEnd)); | 42 result->append(static_cast<unsigned>(lineEnd)); |
| 44 start = lineEnd + 1; | 43 start = lineEnd + 1; |
| 45 } | 44 } |
| 46 result->append(text.length()); | 45 result->append(text.length()); |
| 47 | 46 |
| 48 return result.release(); | 47 return result.release(); |
| 49 } | 48 } |
| 50 | 49 |
| 51 OrdinalNumber TextPosition::toOffset(const Vector<unsigned>& lineEndings) | 50 OrdinalNumber TextPosition::toOffset(const Vector<unsigned>& lineEndings) { |
| 52 { | 51 unsigned lineStartOffset = m_line != OrdinalNumber::first() |
| 53 unsigned lineStartOffset = m_line != OrdinalNumber::first() ? lineEndings.at
(m_line.zeroBasedInt() - 1) + 1 : 0; | 52 ? lineEndings.at(m_line.zeroBasedInt() - 1) + 1 |
| 54 return OrdinalNumber::fromZeroBasedInt(lineStartOffset + m_column.zeroBasedI
nt()); | 53 : 0; |
| 54 return OrdinalNumber::fromZeroBasedInt(lineStartOffset + |
| 55 m_column.zeroBasedInt()); |
| 55 } | 56 } |
| 56 | 57 |
| 57 TextPosition TextPosition::fromOffsetAndLineEndings(unsigned offset, const Vecto
r<unsigned>& lineEndings) | 58 TextPosition TextPosition::fromOffsetAndLineEndings( |
| 58 { | 59 unsigned offset, |
| 59 const unsigned* foundLineEnding = std::lower_bound(lineEndings.begin(), line
Endings.end(), offset); | 60 const Vector<unsigned>& lineEndings) { |
| 60 int lineIndex = foundLineEnding - &lineEndings.at(0); | 61 const unsigned* foundLineEnding = |
| 61 unsigned lineStartOffset = lineIndex > 0 ? lineEndings.at(lineIndex - 1) + 1
: 0; | 62 std::lower_bound(lineEndings.begin(), lineEndings.end(), offset); |
| 62 int column = offset - lineStartOffset; | 63 int lineIndex = foundLineEnding - &lineEndings.at(0); |
| 63 return TextPosition(OrdinalNumber::fromZeroBasedInt(lineIndex), OrdinalNumbe
r::fromZeroBasedInt(column)); | 64 unsigned lineStartOffset = |
| 65 lineIndex > 0 ? lineEndings.at(lineIndex - 1) + 1 : 0; |
| 66 int column = offset - lineStartOffset; |
| 67 return TextPosition(OrdinalNumber::fromZeroBasedInt(lineIndex), |
| 68 OrdinalNumber::fromZeroBasedInt(column)); |
| 64 } | 69 } |
| 65 | 70 |
| 66 } // namespace WTF | 71 } // namespace WTF |
| OLD | NEW |