Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1081)

Unified Diff: Source/core/html/TimeRanges.cpp

Issue 559993004: Fix TimeRanges::nearest() to actually calculate nearest time. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Comments. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/html/TimeRanges.cpp
diff --git a/Source/core/html/TimeRanges.cpp b/Source/core/html/TimeRanges.cpp
index 943eb93468f6899f8510f24fee18dce0a1abbc79..72e1ee9c7052b75849b676c2a19d59b9e23a7368 100644
--- a/Source/core/html/TimeRanges.cpp
+++ b/Source/core/html/TimeRanges.cpp
@@ -182,21 +182,32 @@ bool TimeRanges::contain(double time) const
return false;
}
-double TimeRanges::nearest(double time) const
+double TimeRanges::nearest(double time, double now) const
acolwell GONE FROM CHROMIUM 2014/09/11 22:24:39 ditto.
DaleCurtis 2014/09/11 22:40:01 Done.
{
- double closest = 0;
unsigned count = length();
+ double bestMatch = 0;
+ double bestDelta = std::numeric_limits<double>::infinity();
for (unsigned ndx = 0; ndx < count; ndx++) {
double startTime = start(ndx, IGNORE_EXCEPTION);
double endTime = end(ndx, IGNORE_EXCEPTION);
if (time >= startTime && time <= endTime)
return time;
- if (fabs(startTime - time) < closest)
- closest = fabs(startTime - time);
- else if (fabs(endTime - time) < closest)
- closest = fabs(endTime - time);
+
+ double delta, match;
+ if (time < startTime) {
+ delta = startTime - time;
+ match = startTime;
+ } else {
+ delta = time - endTime;
+ match = endTime;
+ }
+
+ if (delta < bestDelta || (delta == bestDelta && std::abs(now - match) < std::abs(now - bestMatch))) {
+ bestDelta = delta;
+ bestMatch = match;
+ }
}
- return closest;
+ return bestMatch;
}
void TimeRanges::trace(Visitor* visitor)

Powered by Google App Engine
This is Rietveld 408576698