1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/** @file
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "Range.h"
#include "slice.h"
#include <algorithm>
#include <cinttypes>
#include <cctype>
#include <cstdio>
#include <cstring>
bool
Range::isValid() const
{
return m_beg < m_end && (0 <= m_beg || 0 == m_end);
}
int64_t
Range::size() const
{
return m_end - m_beg;
}
bool
Range::fromStringClosed(char const *const rangestr)
{
static char const *const BYTESTR = "bytes=";
static size_t const BYTESTRLEN = strlen(BYTESTR);
m_beg = m_end = -1; // initialize invalid
// make sure this is in byte units
if (0 != strncmp(BYTESTR, rangestr, BYTESTRLEN)) {
return false;
}
// advance past any white space
char const *pstr = rangestr + BYTESTRLEN;
while ('\0' != *pstr && isblank(*pstr)) {
++pstr;
}
// rip out any whitespace
char rangebuf[1024];
int const rangelen = sizeof(rangebuf);
char *pbuf = rangebuf;
while ('\0' != *pstr && (pbuf - rangebuf) < rangelen) {
if (!isblank(*pstr)) {
*pbuf++ = *pstr;
}
++pstr;
}
*pbuf = '\0';
int const rlen = (pbuf - rangebuf);
int consumed = 0;
// last 'n' bytes - result in range with negative begin and 0 end
int64_t endbytes = 0;
char const *const fmtend = "-%" PRId64 "%n";
int const fieldsend = sscanf(rangebuf, fmtend, &endbytes, &consumed);
if (1 == fieldsend) {
if (rlen == consumed) {
m_beg = -endbytes;
m_end = 0;
return true;
} else {
return false;
}
}
// normal range <front>-<back>
char const *const fmtclosed = "%" PRId64 "-%" PRId64 "%n";
int64_t front = 0;
int64_t back = 0;
int const fieldsclosed = sscanf(rangebuf, fmtclosed, &front, &back, &consumed);
if (2 == fieldsclosed) {
if (0 <= front && front <= back && rlen == consumed) {
m_beg = front;
m_end = back + 1;
return true;
} else {
return false;
}
}
front = 0;
char const *const fmtbeg = "%" PRId64 "-%n";
int const fieldsbeg = sscanf(rangebuf, fmtbeg, &front, &consumed);
if (1 == fieldsbeg) {
if (rlen == consumed) {
m_beg = front;
m_end = Range::maxval;
return true;
} else {
return false;
}
}
return false;
} // parseRange
bool
Range::toStringClosed(char *const bufstr,
int *const buflen // returns actual bytes used
) const
{
if (!isValid()) {
if (0 < *buflen) {
bufstr[0] = '\0';
}
*buflen = 0;
return false;
}
int const lenin = *buflen;
if (m_end <= Range::maxval) {
*buflen = snprintf(bufstr, lenin, "bytes=%" PRId64 "-%" PRId64, m_beg, m_end - 1);
} else {
*buflen = snprintf(bufstr, lenin, "bytes=%" PRId64 "-", m_beg);
}
return (0 < *buflen && *buflen < lenin);
}
int64_t
Range::firstBlockFor(int64_t const blocksize) const
{
if (0 < blocksize && isValid()) {
return std::max(static_cast<int64_t>(0), m_beg / blocksize);
} else {
return -1;
}
}
int64_t
Range::lastBlockFor(int64_t const blocksize) const
{
if (0 < blocksize && isValid()) {
return std::max(static_cast<int64_t>(0), (m_end - 1) / blocksize);
} else {
return -1;
}
}
Range
Range::intersectedWith(Range const &other) const
{
return Range(std::max(m_beg, other.m_beg), std::min(m_end, other.m_end));
}
bool
Range::blockIsInside(int64_t const blocksize, int64_t const blocknum) const
{
Range const blockrange(blocksize * blocknum, blocksize * (blocknum + 1));
Range const isec(blockrange.intersectedWith(*this));
return isec.isValid();
}
int64_t
Range::skipBytesForBlock(int64_t const blocksize, int64_t const blocknum) const
{
int64_t const blockstart(blocksize * blocknum);
if (m_beg < blockstart) {
return 0;
} else {
return m_beg - blockstart;
}
}
bool
Range::isEndBytes() const
{
return m_beg < 0 && 0 == m_end;
}
|