2013-01-04 16:01:50 +01:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
2011-02-09 23:31:44 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2013-01-04 16:01:50 +01:00
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2011-02-09 23:31:44 +01:00
|
|
|
|
|
|
|
#include "libaegisub/util.h"
|
2013-10-02 01:27:33 +02:00
|
|
|
#include "libaegisub/util_osx.h"
|
2011-02-09 23:31:44 +01:00
|
|
|
|
2013-01-30 16:26:16 +01:00
|
|
|
#include <boost/locale/boundary.hpp>
|
|
|
|
#include <boost/locale/conversion.hpp>
|
2014-05-23 00:40:16 +02:00
|
|
|
#include <boost/range/distance.hpp>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <ctime>
|
2011-02-09 23:31:44 +01:00
|
|
|
|
2013-01-30 16:26:16 +01:00
|
|
|
namespace {
|
|
|
|
const size_t bad_pos = (size_t)-1;
|
|
|
|
const std::pair<size_t, size_t> bad_match(bad_pos, bad_pos);
|
|
|
|
|
|
|
|
template<typename Iterator>
|
|
|
|
size_t advance_both(Iterator& folded, Iterator& raw) {
|
|
|
|
size_t len;
|
|
|
|
if (*folded == *raw) {
|
|
|
|
len = folded->length();
|
|
|
|
++folded;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// This character was changed by case folding, so refold it and eat the
|
|
|
|
// appropriate number of characters from folded
|
|
|
|
len = boost::locale::fold_case(raw->str()).size();
|
|
|
|
for (size_t folded_consumed = 0; folded_consumed < len; ++folded)
|
|
|
|
folded_consumed += folded->length();
|
|
|
|
}
|
|
|
|
|
|
|
|
++raw;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<size_t, size_t> find_range(std::string const& haystack, std::string const& needle, size_t start = 0) {
|
|
|
|
const size_t match_start = haystack.find(needle, start);
|
|
|
|
if (match_start == std::string::npos)
|
|
|
|
return bad_match;
|
2014-05-25 16:41:21 +02:00
|
|
|
return {match_start, match_start + needle.size()};
|
2013-01-30 16:26:16 +01:00
|
|
|
}
|
|
|
|
|
2015-07-27 17:59:35 +02:00
|
|
|
void parse_blocks(std::vector<std::pair<size_t, size_t>>& blocks, std::string const& str) {
|
|
|
|
blocks.clear();
|
|
|
|
|
|
|
|
size_t ovr_start = bad_pos;
|
|
|
|
size_t i = 0;
|
|
|
|
for (auto const& c : str) {
|
|
|
|
if (c == '{' && ovr_start == bad_pos)
|
|
|
|
ovr_start = i;
|
|
|
|
else if (c == '}' && ovr_start != bad_pos) {
|
2015-07-27 18:46:28 +02:00
|
|
|
blocks.emplace_back(ovr_start, i + 1);
|
2015-07-27 17:59:35 +02:00
|
|
|
ovr_start = bad_pos;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
2013-01-30 16:26:16 +01:00
|
|
|
}
|
|
|
|
|
2015-07-27 17:59:35 +02:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
namespace agi { namespace util {
|
2011-02-10 01:41:15 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string strftime(const char *fmt, const tm *tmptr) {
|
|
|
|
if (!tmptr) {
|
|
|
|
time_t t = time(nullptr);
|
|
|
|
tmptr = localtime(&t);
|
|
|
|
}
|
2011-02-10 01:41:15 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
char buff[65536];
|
|
|
|
::strftime(buff, sizeof buff, fmt, tmptr);
|
|
|
|
return buff;
|
2011-02-10 01:41:15 +01:00
|
|
|
}
|
|
|
|
|
2013-01-30 16:26:16 +01:00
|
|
|
std::pair<size_t, size_t> ifind(std::string const& haystack, std::string const& needle) {
|
|
|
|
const auto folded_hs = boost::locale::fold_case(haystack);
|
|
|
|
const auto folded_n = boost::locale::fold_case(needle);
|
|
|
|
auto match = find_range(folded_hs, folded_n);
|
|
|
|
if (match == bad_match || folded_hs == haystack)
|
|
|
|
return match;
|
|
|
|
|
|
|
|
// We have a match, but the position is an index into the folded string
|
|
|
|
// and we want an index into the unfolded string.
|
|
|
|
|
|
|
|
using namespace boost::locale::boundary;
|
|
|
|
const ssegment_index haystack_characters(character, begin(haystack), end(haystack));
|
|
|
|
const ssegment_index folded_characters(character, begin(folded_hs), end(folded_hs));
|
|
|
|
const size_t haystack_char_count = boost::distance(haystack_characters);
|
|
|
|
const size_t folded_char_count = boost::distance(folded_characters);
|
|
|
|
|
|
|
|
// As of Unicode 6.2, case folding can never reduce the number of
|
|
|
|
// characters, and can only reduce the number of bytes with UTF-8 when
|
|
|
|
// increasing the number of characters. As a result, iff the bytes and
|
|
|
|
// characters are unchanged, no folds changed the size of any characters
|
|
|
|
// and our indices are correct.
|
|
|
|
if (haystack.size() == folded_hs.size() && haystack_char_count == folded_char_count)
|
|
|
|
return match;
|
|
|
|
|
|
|
|
const auto map_folded_to_raw = [&]() -> std::pair<size_t, size_t> {
|
|
|
|
size_t start = -1;
|
|
|
|
|
|
|
|
// Iterate over each pair of characters and refold each character which was
|
|
|
|
// changed by folding, so that we can find the corresponding positions in
|
|
|
|
// the unfolded string
|
|
|
|
auto folded_it = begin(folded_characters);
|
|
|
|
auto haystack_it = begin(haystack_characters);
|
|
|
|
size_t folded_pos = 0;
|
|
|
|
|
|
|
|
while (folded_pos < match.first)
|
|
|
|
folded_pos += advance_both(folded_it, haystack_it);
|
|
|
|
// If we overshot the start then the match started in the middle of a
|
|
|
|
// character which was folded to multiple characters
|
|
|
|
if (folded_pos > match.first)
|
|
|
|
return bad_match;
|
|
|
|
|
|
|
|
start = distance(begin(haystack), begin(*haystack_it));
|
|
|
|
|
|
|
|
while (folded_pos < match.second)
|
|
|
|
folded_pos += advance_both(folded_it, haystack_it);
|
|
|
|
if (folded_pos > match.second)
|
|
|
|
return bad_match;
|
|
|
|
|
2014-05-25 16:41:21 +02:00
|
|
|
return {start, distance(begin(haystack), begin(*haystack_it))};
|
2013-01-30 16:26:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
auto ret = map_folded_to_raw();
|
|
|
|
while (ret == bad_match) {
|
|
|
|
// Found something, but it was an invalid match so retry from the next character
|
|
|
|
match = find_range(folded_hs, folded_n, match.first + 1);
|
|
|
|
if (match == bad_match) return match;
|
|
|
|
ret = map_folded_to_raw();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-27 17:59:35 +02:00
|
|
|
std::string tagless_find_helper::strip_tags(std::string const& str, size_t s) {
|
|
|
|
parse_blocks(blocks, str);
|
|
|
|
|
|
|
|
std::string out;
|
|
|
|
|
|
|
|
size_t last = s;
|
|
|
|
for (auto const& block : blocks) {
|
2015-07-27 18:46:28 +02:00
|
|
|
if (block.second <= s) continue;
|
2015-07-27 17:59:35 +02:00
|
|
|
if (block.first > last)
|
|
|
|
out.append(str.begin() + last, str.begin() + block.first);
|
2015-07-27 18:46:28 +02:00
|
|
|
last = block.second;
|
2015-07-27 17:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (last < str.size())
|
|
|
|
out.append(str.begin() + last, str.end());
|
|
|
|
|
|
|
|
start = s;
|
|
|
|
return out;
|
2013-10-02 01:27:33 +02:00
|
|
|
}
|
|
|
|
|
2015-07-27 17:59:35 +02:00
|
|
|
void tagless_find_helper::map_range(size_t &s, size_t &e) {
|
|
|
|
s += start;
|
|
|
|
e += start;
|
|
|
|
|
|
|
|
// Shift the start and end of the match to be relative to the unstripped
|
|
|
|
// match
|
|
|
|
for (auto const& block : blocks) {
|
|
|
|
// Any blocks before start are irrelevant as they're included in `start`
|
2019-09-11 17:16:51 +02:00
|
|
|
if (block.second <= start) continue;
|
2015-07-27 17:59:35 +02:00
|
|
|
// Skip over blocks at the very beginning of the match
|
|
|
|
// < should only happen if the cursor was within an override block
|
|
|
|
// when the user started a search
|
|
|
|
if (block.first <= s) {
|
2015-07-27 18:46:28 +02:00
|
|
|
size_t len = block.second - std::max(block.first, start);
|
2015-07-27 17:59:35 +02:00
|
|
|
s += len;
|
|
|
|
e += len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blocks after the match are irrelevant
|
|
|
|
if (block.first >= e) break;
|
|
|
|
|
|
|
|
// Extend the match to include blocks within the match
|
|
|
|
// Note that blocks cannot be partially within the match
|
2015-07-27 18:46:28 +02:00
|
|
|
e += block.second - block.first;
|
2015-07-27 17:59:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace util
|
|
|
|
|
2013-10-02 01:27:33 +02:00
|
|
|
#ifndef __APPLE__
|
|
|
|
namespace osx {
|
2014-03-26 17:24:47 +01:00
|
|
|
AppNapDisabler::AppNapDisabler(std::string reason) { }
|
2013-10-02 01:27:33 +02:00
|
|
|
AppNapDisabler::~AppNapDisabler() { }
|
|
|
|
}
|
|
|
|
#endif
|
2015-07-27 17:59:35 +02:00
|
|
|
} // namespace agi
|