restyle intern module to match project style

Reduces lint errors from 8 to 6 (-25%). Line count from 112 to 83 (-26%).

Another step in resolving issue #2902.
This commit is contained in:
Kurtis Rader 2016-05-01 20:26:32 -07:00
parent da17420cdf
commit 8b2cf81f17
2 changed files with 35 additions and 64 deletions

View file

@ -1,72 +1,59 @@
/** \file intern.c // Library for pooling common strings.
Library for pooling common strings
*/
#include <wchar.h>
#include <pthread.h> #include <pthread.h>
#include <vector> #include <stdbool.h>
#include <stddef.h>
#include <wchar.h>
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
#include <stddef.h> #include <vector>
#include <stdbool.h>
#include "fallback.h" // IWYU pragma: keep
#include "common.h" #include "common.h"
#include "fallback.h" // IWYU pragma: keep
#include "intern.h" #include "intern.h"
/** Comparison function for intern'd strings */ /// Comparison function for intern'd strings.
class string_table_compare_t class string_table_compare_t {
{ public:
public: bool operator()(const wchar_t *a, const wchar_t *b) const { return wcscmp(a, b) < 0; }
bool operator()(const wchar_t *a, const wchar_t *b) const
{
return wcscmp(a, b) < 0;
}
}; };
/* A sorted vector ends up being a little more memory efficient than a std::set for the intern'd string table */ // A sorted vector ends up being a little more memory efficient than a std::set for the intern'd
// string table.
#define USE_SET 0 #define USE_SET 0
#if USE_SET #if USE_SET
/** The table of intern'd strings */ /// The table of intern'd strings.
typedef std::set<const wchar_t *, string_table_compare_t> string_table_t; typedef std::set<const wchar_t *, string_table_compare_t> string_table_t;
#else #else
/** The table of intern'd strings */ /// The table of intern'd strings.
typedef std::vector<const wchar_t *> string_table_t; typedef std::vector<const wchar_t *> string_table_t;
#endif #endif
static string_table_t string_table; static string_table_t string_table;
/** The lock to provide thread safety for intern'd strings */ /// The lock to provide thread safety for intern'd strings.
static pthread_mutex_t intern_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t intern_lock = PTHREAD_MUTEX_INITIALIZER;
static const wchar_t *intern_with_dup(const wchar_t *in, bool dup) static const wchar_t *intern_with_dup(const wchar_t *in, bool dup) {
{ if (!in) return NULL;
if (!in)
return NULL;
// debug( 0, L"intern %ls", in ); // debug( 0, L"intern %ls", in );
scoped_lock lock(intern_lock); scoped_lock lock(intern_lock);
const wchar_t *result; const wchar_t *result;
#if USE_SET #if USE_SET
string_table_t::const_iterator iter = string_table.find(in); string_table_t::const_iterator iter = string_table.find(in);
if (iter != string_table.end()) if (iter != string_table.end()) {
{
result = *iter; result = *iter;
} } else {
else
{
result = dup ? wcsdup(in) : in; result = dup ? wcsdup(in) : in;
string_table.insert(result); string_table.insert(result);
} }
#else #else
string_table_t::iterator iter = std::lower_bound(string_table.begin(), string_table.end(), in, string_table_compare_t()); string_table_t::iterator iter =
if (iter != string_table.end() && wcscmp(*iter, in) == 0) std::lower_bound(string_table.begin(), string_table.end(), in, string_table_compare_t());
{ if (iter != string_table.end() && wcscmp(*iter, in) == 0) {
result = *iter; result = *iter;
} } else {
else
{
result = dup ? wcsdup(in) : in; result = dup ? wcsdup(in) : in;
string_table.insert(iter, result); string_table.insert(iter, result);
} }
@ -74,13 +61,6 @@ static const wchar_t *intern_with_dup(const wchar_t *in, bool dup)
return result; return result;
} }
const wchar_t *intern(const wchar_t *in) const wchar_t *intern(const wchar_t *in) { return intern_with_dup(in, true); }
{
return intern_with_dup(in, true);
}
const wchar_t *intern_static(const wchar_t *in) { return intern_with_dup(in, false); }
const wchar_t *intern_static(const wchar_t *in)
{
return intern_with_dup(in, false);
}

View file

@ -1,26 +1,17 @@
/** \file intern.h // Library for pooling common strings.
Library for pooling common strings
*/
#ifndef FISH_INTERN_H #ifndef FISH_INTERN_H
#define FISH_INTERN_H #define FISH_INTERN_H
/** /// Return an identical copy of the specified string from a pool of unique strings. If the string
Return an identical copy of the specified string from a pool of unique strings. If the string was not in the pool, add a copy. /// was not in the pool, add a copy.
///
\param in the string to return an interned copy of /// \param in the string to return an interned copy of.
*/
const wchar_t *intern(const wchar_t *in); const wchar_t *intern(const wchar_t *in);
/** /// Insert the specified string literal into the pool of unique strings. The string will not first
Insert the specified string literal into the pool of unique /// be copied, and it will not be free'd on exit.
strings. The string will not first be copied, and it will not be ///
free'd on exit. /// \param in the string to add to the interned pool
\param in the string to add to the interned pool
*/
const wchar_t *intern_static(const wchar_t *in); const wchar_t *intern_static(const wchar_t *in);
#endif #endif