| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/sksl/SkSLPosition.h" |
| 9 |
|
| 10 |
#include <algorithm> |
| 11 |
|
| 12 |
namespace SkSL { |
| 13 |
|
| 14 |
int Position::line(std::string_view source) const { |
| 15 |
SkASSERT(this->valid()); |
| 16 |
if (fStartOffset == -1) { |
| 17 |
return -1; |
| 18 |
} |
| 19 |
if (!source.data()) { |
| 20 |
return -1; |
| 21 |
} |
| 22 |
// we allow the offset to equal the length, because that's where TK_END_OF_FILE is reported |
| 23 |
SkASSERT(fStartOffset <= (int)source.length()); |
| 24 |
int offset = std::min(fStartOffset, (int)source.length()); |
| 25 |
int line = 1; |
| 26 |
for (int i = 0; i < offset; i++) { |
| 27 |
if (source[i] == '\n') { |
| 28 |
++line; |
| 29 |
} |
| 30 |
} |
| 31 |
return line; |
| 32 |
} |
| 33 |
|
| 34 |
} // namespace SkSL |