sapling/eden/fs/utils/ChronoUnit.cpp
Chad Austin 9fa292b9ed standardize namespaces on C++17 syntax
Reviewed By: genevievehelsel

Differential Revision: D36429182

fbshipit-source-id: 7d355917abf463493c37139856810de13e1090ff
2022-05-17 10:12:56 -07:00

167 lines
4.7 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#include "eden/fs/utils/ChronoUnit.h"
/*
* This file was originally generated by gperf-3.1, but then hand-tweaked
* to work more nicely with folly::StringPiece.
* https://www.gnu.org/software/gperf/
*
* C++ code produced by gperf version 3.1
* Command-line: gperf eden/fs/utils/ChronoUnit.gperf
* Computed positions: -k'1-3'
*/
using facebook::eden::ChronoUnit;
using folly::StringPiece;
namespace {
#define TOTAL_KEYWORDS 33
#define MIN_WORD_LENGTH 1
#define MAX_WORD_LENGTH 12
#define MIN_HASH_VALUE 1
#define MAX_HASH_VALUE 69
/* maximum key range = 69, duplicates = 0 */
class ChronoUnitPerfectHash {
private:
static inline unsigned int hash(StringPiece str);
public:
static const ChronoUnit* FOLLY_NULLABLE in_word_set(StringPiece str);
};
inline unsigned int ChronoUnitPerfectHash::hash(StringPiece str) {
static constexpr std::array<unsigned char, 256> asso_values{
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 30, 70, 5, 35, 20, 70, 70, 20, 5, 70, 35,
25, 5, 0, 0, 70, 70, 20, 0, 70, 10, 70, 0, 70, 0, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 0, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 0, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70,
70, 70, 70, 70};
unsigned int hval = str.size();
switch (hval) {
default:
hval += asso_values[static_cast<unsigned char>(str[2])];
FOLLY_FALLTHROUGH;
case 2:
hval += asso_values[static_cast<unsigned char>(str[1])];
FOLLY_FALLTHROUGH;
case 1:
hval += asso_values[static_cast<unsigned char>(str[0])];
break;
}
return hval;
}
const ChronoUnit* FOLLY_NULLABLE
ChronoUnitPerfectHash::in_word_set(StringPiece str) {
static constexpr std::array<ChronoUnit, MAX_HASH_VALUE + 1> wordlist{
{{"", 0, 0},
{"s", 1, 1},
{"ns", 1, 1000000000},
{"\316\274s", 1, 1000000},
{"", 0, 0},
{"", 0, 0},
{"m", 60, 1},
{"ms", 1, 1000},
{"mon", 2629746, 1},
{"", 0, 0},
{"month", 2629746, 1},
{"months", 2629746, 1},
{"us", 1, 1000000},
{"min", 60, 1},
{"", 0, 0},
{"", 0, 0},
{"minute", 60, 1},
{"minutes", 60, 1},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"h", 3600, 1},
{"yr", 31556952, 1},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"microsecond", 1, 1000000},
{"microseconds", 1, 1000000},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"second", 1, 1},
{"seconds", 1, 1},
{"", 0, 0},
{"hour", 3600, 1},
{"hours", 3600, 1},
{"d", 86400, 1},
{"wk", 604800, 1},
{"", 0, 0},
{"", 0, 0},
{"nanosecond", 1, 1000000000},
{"nanoseconds", 1, 1000000000},
{"hr", 3600, 1},
{"", 0, 0},
{"week", 604800, 1},
{"weeks", 604800, 1},
{"millisecond", 1, 1000},
{"milliseconds", 1, 1000},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"year", 31556952, 1},
{"years", 31556952, 1},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"", 0, 0},
{"day", 86400, 1},
{"days", 86400, 1}}};
if (str.size() <= MAX_WORD_LENGTH && str.size() >= MIN_WORD_LENGTH) {
unsigned int key = hash(str);
if (key <= MAX_HASH_VALUE) {
if (wordlist[key].name == str) {
return &wordlist[key];
}
}
}
return nullptr;
}
} // unnamed namespace
namespace facebook::eden {
const ChronoUnit* FOLLY_NULLABLE lookupChronoUnitInfo(StringPiece unitName) {
return ChronoUnitPerfectHash::in_word_set(unitName);
}
} // namespace facebook::eden