-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #857 from Expensify/master
Update expensify_prod branch
- Loading branch information
Showing
17 changed files
with
153 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <libstuff/libstuff.h> | ||
|
||
SFastBuffer::SFastBuffer() : front(0) { | ||
} | ||
|
||
SFastBuffer::SFastBuffer(const string& str) : front(0), data(str) { | ||
} | ||
|
||
bool SFastBuffer::empty() const { | ||
return size() == 0; | ||
} | ||
|
||
size_t SFastBuffer::size() const { | ||
return data.size() - front; | ||
} | ||
|
||
const char* SFastBuffer::c_str() const { | ||
return data.data() + front; | ||
} | ||
|
||
void SFastBuffer::clear() { | ||
front = 0; | ||
data.clear(); | ||
} | ||
|
||
void SFastBuffer::consumeFront(size_t bytes) { | ||
front += bytes; | ||
|
||
// If we're all caught up, reset. | ||
if (front == data.size()) { | ||
clear(); | ||
} | ||
} | ||
|
||
void SFastBuffer::append(const char* buffer, size_t bytes) { | ||
// When will we condense everything to the front of the buffer? | ||
// When: | ||
// 1. We're not already at the front of the buffer (this implies there's data in the buffer). | ||
// 2. We'd have to do a realloc anyway because our buffer's not big enough for the new string (including the | ||
// existing consumed buffer). | ||
if (front && (data.capacity() - data.size() < bytes)) { | ||
memmove(&data[0], data.data() + front, size()); | ||
data.resize(size()); | ||
front = 0; | ||
|
||
// If the capacity is more than 4x the size we need, let's give some memory back. | ||
if (data.capacity() > (data.size() + bytes) * 4) { | ||
data.shrink_to_fit(); | ||
} | ||
} | ||
|
||
// After the resize, we may or may not need to actually reallocate. We can append now and let the string | ||
// implementation decide if it needs more room. | ||
data.append(buffer, bytes); | ||
} | ||
|
||
SFastBuffer& SFastBuffer::operator+=(const string& rhs) { | ||
append(rhs.c_str(), rhs.size()); | ||
return *this; | ||
} | ||
|
||
SFastBuffer& SFastBuffer::operator=(const string& rhs) { | ||
front = 0; | ||
data = rhs; | ||
return *this; | ||
} | ||
|
||
ostream& operator<<(ostream& os, const SFastBuffer& buf) | ||
{ | ||
os << buf.c_str(); | ||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
class SFastBuffer { | ||
public: | ||
SFastBuffer(); | ||
SFastBuffer(const string& str); | ||
bool empty() const; | ||
size_t size() const; | ||
const char* c_str() const; | ||
void clear(); | ||
void consumeFront(size_t bytes); | ||
void append(const char* buffer, size_t bytes); | ||
SFastBuffer& operator+=(const string& rhs); | ||
SFastBuffer& operator=(const string& rhs); | ||
|
||
private: | ||
size_t front; | ||
string data; | ||
}; | ||
ostream& operator<<(ostream& os, const SFastBuffer& buf); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.