Make AssStyle::Clone ~15 times faster.

Originally committed to SVN as r4664.
This commit is contained in:
Thomas Goyne 2010-07-08 07:14:55 +00:00
parent a59d2a8e2e
commit 1da57a4be5
8 changed files with 116 additions and 202 deletions

View file

@ -214,7 +214,7 @@ bool AssDialogue::Parse(wxString rawData, int version) {
return true; return true;
} }
wxString AssDialogue::GetData(bool ssa) { wxString AssDialogue::GetData(bool ssa) const {
wxString final; wxString final;
if (Comment) final = L"Comment: "; if (Comment) final = L"Comment: ";
@ -228,10 +228,13 @@ wxString AssDialogue::GetData(bool ssa) {
final += Start.GetASSFormated() + L","; final += Start.GetASSFormated() + L",";
final += End.GetASSFormated() + L","; final += End.GetASSFormated() + L",";
Style.Replace(L",",L";"); wxString s = Style;
Actor.Replace(L",",L";"); wxString a = Actor;
final += Style + L","; wxString e = Effect;
final += Actor + L","; s.Replace(L",",L";");
a.Replace(L",",L";");
final += s + L",";
final += a + L",";
final += GetMarginString(0); final += GetMarginString(0);
final += L","; final += L",";
@ -240,8 +243,8 @@ wxString AssDialogue::GetData(bool ssa) {
final += GetMarginString(2); final += GetMarginString(2);
final += L","; final += L",";
Effect.Replace(L",",L";"); e.Replace(L",",L";");
final += Effect + L","; final += e + L",";
final += Text; final += Text;
// Make sure that final has no line breaks // Make sure that final has no line breaks
@ -251,11 +254,11 @@ wxString AssDialogue::GetData(bool ssa) {
return final; return final;
} }
const wxString AssDialogue::GetEntryData() { const wxString AssDialogue::GetEntryData() const {
return GetData(false); return GetData(false);
} }
wxString AssDialogue::GetSSAText () { wxString AssDialogue::GetSSAText () const {
return GetData(true); return GetData(true);
} }
@ -643,7 +646,7 @@ void AssDialogue::SetMarginString(const wxString origvalue,int which) {
Margin[which] = value; Margin[which] = value;
} }
wxString AssDialogue::GetMarginString(int which,bool pad) { wxString AssDialogue::GetMarginString(int which,bool pad) const {
if (which < 0 || which >= 4) throw Aegisub::InvalidMarginIdError(); if (which < 0 || which >= 4) throw Aegisub::InvalidMarginIdError();
int value = Margin[which]; int value = Margin[which];
if (pad) return wxString::Format(_T("%04i"),value); if (pad) return wxString::Format(_T("%04i"),value);

View file

@ -163,7 +163,7 @@ public:
/// ///
/// DOCME /// DOCME
class AssDialogue : public AssEntry { class AssDialogue : public AssEntry {
wxString GetData(bool ssa); wxString GetData(bool ssa) const;
public: public:
/// Contains information about each block of text /// Contains information about each block of text
@ -188,7 +188,7 @@ public:
/// Raw text data /// Raw text data
wxString Text; wxString Text;
ASS_EntryType GetType() { return ENTRY_DIALOGUE; } ASS_EntryType GetType() const { return ENTRY_DIALOGUE; }
/// @brief Parse raw ASS data into everything else /// @brief Parse raw ASS data into everything else
/// @param data ASS line /// @param data ASS line
@ -217,7 +217,7 @@ public:
/// If blocks have been parsed, update the text from their current value /// If blocks have been parsed, update the text from their current value
void UpdateText(); void UpdateText();
const wxString GetEntryData(); const wxString GetEntryData() const;
/// Do nothing /// Do nothing
void SetEntryData(wxString) { } void SetEntryData(wxString) { }
/// Synonym for ClearBlocks /// Synonym for ClearBlocks
@ -231,9 +231,9 @@ public:
/// @brief Get a margin /// @brief Get a margin
/// @param which 0 = left, 1 = right, 2 = vertical/top, 3 = bottom /// @param which 0 = left, 1 = right, 2 = vertical/top, 3 = bottom
/// @param pad Pad the number to four digits /// @param pad Pad the number to four digits
wxString GetMarginString(int which,bool pad=true); wxString GetMarginString(int which,bool pad=true) const;
/// Get the line as SSA rather than ASS /// Get the line as SSA rather than ASS
wxString GetSSAText(); wxString GetSSAText() const;
/// Does this line collide with the passed line? /// Does this line collide with the passed line?
bool CollidesWith(AssDialogue *target); bool CollidesWith(AssDialogue *target);

View file

@ -66,7 +66,7 @@ AssEntry::~AssEntry() {
/// @brief Get SSA conversion /// @brief Get SSA conversion
/// @return /// @return
/// ///
wxString AssEntry::GetSSAText() { wxString AssEntry::GetSSAText() const {
// Special cases // Special cases
if (data.Lower() == _T("[v4+ styles]")) return wxString(_T("[V4 Styles]")); if (data.Lower() == _T("[v4+ styles]")) return wxString(_T("[V4 Styles]"));
if (data.Lower() == _T("scripttype: v4.00+")) return wxString(_T("ScriptType: v4.00")); if (data.Lower() == _T("scripttype: v4.00+")) return wxString(_T("ScriptType: v4.00"));

View file

@ -125,17 +125,17 @@ public:
/// @brief DOCME /// @brief DOCME
/// @return /// @return
/// ///
virtual ASS_EntryType GetType() { return ENTRY_BASE; } virtual ASS_EntryType GetType() const { return ENTRY_BASE; }
/// @brief DOCME /// @brief DOCME
/// @return /// @return
/// ///
virtual const wxString GetEntryData() { return data; } virtual const wxString GetEntryData() const { return data; }
/// @brief DOCME /// @brief DOCME
/// @param newData /// @param newData
/// ///
virtual void SetEntryData(wxString newData) { if (newData.IsEmpty()) data.Clear(); else data = newData; } virtual void SetEntryData(wxString newData) { data = newData; }
virtual wxString GetSSAText(); virtual wxString GetSSAText() const;
}; };

View file

@ -34,8 +34,6 @@
/// @ingroup subs_storage /// @ingroup subs_storage
/// ///
////////////
// Includes
#include "config.h" #include "config.h"
#ifndef AGI_PRE #ifndef AGI_PRE
@ -48,27 +46,26 @@
#include "ass_style.h" #include "ass_style.h"
#include "utils.h" #include "utils.h"
/// @brief Constructors AssColor //////////////////////////
///
AssColor::AssColor () { AssColor::AssColor () {
r=g=b=a=0; r=g=b=a=0;
} }
AssColor::AssColor(int r, int g, int b, int a)
: r(r)
, g(g)
, b(b)
, a(a)
{
}
/// @brief DOCME /// @brief DOCME
/// @param color /// @param color
/// ///
AssColor::AssColor (wxColour &color) { AssColor::AssColor (const wxColour &color) {
SetWXColor(color); SetWXColor(color);
} }
/// @brief Parse from SSA/ASS /// @brief Parse from SSA/ASS
/// @param value /// @param value
/// @return
///
void AssColor::Parse(const wxString value) { void AssColor::Parse(const wxString value) {
if (value.Len() > 0 && value[0] == _T('#')) { if (value.Len() > 0 && value[0] == _T('#')) {
// HTML colour // HTML colour
@ -101,20 +98,14 @@ void AssColor::Parse(const wxString value) {
a = (outval>>24)& 0xFF; a = (outval>>24)& 0xFF;
} }
/// @brief Gets a wxColour /// @brief Gets a wxColour
/// @return /// @return
///
wxColour AssColor::GetWXColor() { wxColour AssColor::GetWXColor() {
return wxColour(r,g,b,255-a); return wxColour(r,g,b,255-a);
} }
/// @brief Sets color from wx /// @brief Sets color from wx
/// @param color /// @param color
///
void AssColor::SetWXColor(const wxColor &color) { void AssColor::SetWXColor(const wxColor &color) {
r = color.Red(); r = color.Red();
g = color.Green(); g = color.Green();
@ -122,15 +113,12 @@ void AssColor::SetWXColor(const wxColor &color) {
//a = color.Alpha(); //a = color.Alpha();
} }
/// @brief Get formatted in ASS format /// @brief Get formatted in ASS format
/// @param alpha /// @param alpha
/// @param stripped /// @param stripped
/// @param isStyle /// @param isStyle
/// @return /// @return
/// wxString AssColor::GetASSFormatted(bool alpha,bool stripped,bool isStyle) const {
wxString AssColor::GetASSFormatted (bool alpha,bool stripped,bool isStyle) {
wxString work; wxString work;
if (!stripped) work += _T("&H"); if (!stripped) work += _T("&H");
if (alpha) work += wxString::Format(_T("%02X"),a); if (alpha) work += wxString::Format(_T("%02X"),a);
@ -139,93 +127,80 @@ wxString AssColor::GetASSFormatted (bool alpha,bool stripped,bool isStyle) {
return work; return work;
} }
/// @brief Get decimal formatted /// @brief Get decimal formatted
/// @return /// @return
/// wxString AssColor::GetSSAFormatted() const {
wxString AssColor::GetSSAFormatted () {
long color = (a<<24)+(b<<16)+(g<<8)+r; long color = (a<<24)+(b<<16)+(g<<8)+r;
wxString output=wxString::Format(_T("%i"),(long)color); wxString output=wxString::Format(_T("%i"),(long)color);
return output; return output;
} }
bool AssColor::operator==(const AssColor &col) const {
/// @brief Operators
/// @param col
/// @return
///
bool AssColor::operator==(AssColor &col) const {
return r==col.r && g==col.g && b==col.b && a==col.a; return r==col.r && g==col.g && b==col.b && a==col.a;
} }
/// @brief DOCME bool AssColor::operator!=(const AssColor &col) const {
/// @param col return !(*this == col);
/// @return
///
bool AssColor::operator!=(AssColor &col) const {
return r!=col.r || g!=col.g || b!=col.b || a!=col.a;
} }
AssStyle::AssStyle()
: name(L"Default")
, font(L"Arial")
/// @brief Default Constructor AssStyle ///////////////////////// , fontsize(20.)
/// , primary(255, 255, 255)
AssStyle::AssStyle() { , secondary(255, 0, 0)
group = _T("[V4+ Styles]"); , outline(0, 0, 0)
, shadow(0, 0, 0)
name = _T("Default"); , bold(false)
font = _T("Arial"); , italic(false)
fontsize = 20; , underline(false)
, strikeout(false)
primary.r = 255; , scalex(100.)
primary.g = 255; , scaley(100.)
primary.b = 255; , spacing(0.)
primary.a = 0; , angle(0.)
secondary.r = 255; , borderstyle(1)
secondary.g = 0; , outline_w(2.)
secondary.b = 0; , shadow_w(2.)
secondary.a = 0; , alignment(2)
outline.r = 0; , encoding(1)
outline.g = 0; , relativeTo(1)
outline.b = 0; {
outline.a = 0; group = L"[V4+ Styles]";
shadow.r = 0; for (int i = 0; i < 4; i++)
shadow.g = 0; Margin[i] = 10;
shadow.b = 0;
shadow.a = 0;
bold = false;
italic = false;
underline = false;
strikeout = false;
scalex = 100;
scaley = 100;
spacing = 0;
angle = 0.0;
borderstyle = 1;
outline_w = 2.0;
shadow_w = 2.0;
alignment = 2;
Margin[0] = 10;
Margin[1] = 10;
Margin[2] = 10;
Margin[3] = 10;
encoding = 1;
relativeTo = 1;
UpdateData(); UpdateData();
} }
AssStyle::AssStyle(const AssStyle& s)
: name(s.name)
, font(s.font)
, fontsize(s.fontsize)
, primary(s.primary)
, secondary(s.secondary)
, outline(s.outline)
, shadow(s.shadow)
, bold(s.bold)
, italic(s.italic)
, underline(s.underline)
, strikeout(s.strikeout)
, scalex(s.scalex)
, scaley(s.scaley)
, spacing(s.spacing)
, angle(s.angle)
, borderstyle(s.borderstyle)
, outline_w(s.outline_w)
, shadow_w(s.outline_w)
, alignment(s.alignment)
, encoding(s.encoding)
, relativeTo(s.relativeTo)
{
group = L"[V4+ Styles]";
memcpy(Margin, s.Margin, sizeof(Margin));
SetEntryData(s.GetEntryData());
}
/// @brief Constructor
/// @param _data
/// @param version
///
AssStyle::AssStyle(wxString _data,int version) { AssStyle::AssStyle(wxString _data,int version) {
Valid = Parse(_data,version); Valid = Parse(_data,version);
if (!Valid) { if (!Valid) {
@ -234,15 +209,9 @@ AssStyle::AssStyle(wxString _data,int version) {
UpdateData(); UpdateData();
} }
/// @brief Destructor
///
AssStyle::~AssStyle() { AssStyle::~AssStyle() {
} }
/// @brief Parses value from ASS data /// @brief Parses value from ASS data
/// @param rawData /// @param rawData
/// @param version /// @param version
@ -449,8 +418,6 @@ bool AssStyle::Parse(wxString rawData,int version) {
return true; return true;
} }
/// @brief Writes data back to ASS (v4+) format /// @brief Writes data back to ASS (v4+) format
/// ///
void AssStyle::UpdateData() { void AssStyle::UpdateData() {
@ -475,8 +442,6 @@ void AssStyle::UpdateData() {
SetEntryData(final); SetEntryData(final);
} }
/// @brief Sets margin from a string /// @brief Sets margin from a string
/// @param str /// @param str
/// @param which /// @param which
@ -492,24 +457,20 @@ void AssStyle::SetMarginString(const wxString str,int which) {
Margin[which] = value; Margin[which] = value;
} }
/// @brief Gets string for margin /// @brief Gets string for margin
/// @param which /// @param which
/// @return /// @return
/// ///
wxString AssStyle::GetMarginString(int which) { wxString AssStyle::GetMarginString(int which) const {
if (which < 0 || which >= 4) throw Aegisub::InvalidMarginIdError(); if (which < 0 || which >= 4) throw Aegisub::InvalidMarginIdError();
wxString result = wxString::Format(_T("%04i"),Margin[which]); wxString result = wxString::Format(_T("%04i"),Margin[which]);
return result; return result;
} }
/// @brief Convert style to SSA string /// @brief Convert style to SSA string
/// @return /// @return
/// ///
wxString AssStyle::GetSSAText() { wxString AssStyle::GetSSAText() const {
wxString output; wxString output;
int align = 0; int align = 0;
switch (alignment) { switch (alignment) {
@ -523,11 +484,13 @@ wxString AssStyle::GetSSAText() {
case 8: align = 6; break; case 8: align = 6; break;
case 9: align = 7; break; case 9: align = 7; break;
} }
name.Replace(_T(","),_T(";")); wxString n = name;
font.Replace(_T(","),_T(";")); n.Replace(L",", L";");
wxString f = font;
f.Replace(L",", L";");
output = wxString::Format(_T("Style: %s,%s,%g,%s,%s,0,%s,%d,%d,%d,%g,%g,%d,%d,%d,%d,0,%i"), output = wxString::Format(_T("Style: %s,%s,%g,%s,%s,0,%s,%d,%d,%d,%g,%g,%d,%d,%d,%d,0,%i"),
name.c_str(), font.c_str(), fontsize, n.c_str(), f.c_str(), fontsize,
primary.GetSSAFormatted().c_str(), primary.GetSSAFormatted().c_str(),
secondary.GetSSAFormatted().c_str(), secondary.GetSSAFormatted().c_str(),
shadow.GetSSAFormatted().c_str(), shadow.GetSSAFormatted().c_str(),
@ -538,47 +501,11 @@ wxString AssStyle::GetSSAText() {
return output; return output;
} }
/// @brief Clone /// @brief Clone
/// @return /// @return
/// ///
AssEntry *AssStyle::Clone() const { AssEntry *AssStyle::Clone() const {
// Create clone return new AssStyle(*this);
AssStyle *final = new AssStyle();
// Copy data
final->group = group;
final->Valid = Valid;
final->alignment = alignment;
final->angle = angle;
final->bold = bold;
final->borderstyle = borderstyle;
final->encoding = encoding;
final->font = font;
final->fontsize = fontsize;
final->italic = italic;
final->Margin[0] = Margin[0];
final->Margin[1] = Margin[1];
final->Margin[2] = Margin[2];
final->Margin[3] = Margin[3];
final->name = name;
final->outline = outline;
final->outline_w = outline_w;
final->primary = primary;
final->scalex = scalex;
final->scaley = scaley;
final->secondary = secondary;
final->shadow = shadow;
final->shadow_w = shadow_w;
final->spacing = spacing;
final->strikeout = strikeout;
final->underline = underline;
final->relativeTo = relativeTo;
final->SetEntryData(const_cast<AssStyle*>(this)->GetEntryData());
// Return
return final;
} }
@ -587,7 +514,7 @@ AssEntry *AssStyle::Clone() const {
/// @param style /// @param style
/// @return /// @return
/// ///
bool AssStyle::IsEqualTo(AssStyle *style) { bool AssStyle::IsEqualTo(AssStyle *style) const {
// memcmp won't work because strings won't match // memcmp won't work because strings won't match
if (style->alignment != alignment || if (style->alignment != alignment ||
style->angle != angle || style->angle != angle ||

View file

@ -34,53 +34,37 @@
/// @ingroup subs_storage /// @ingroup subs_storage
/// ///
///////////
// Headers
#ifndef AGI_PRE #ifndef AGI_PRE
#include <wx/colour.h> #include <wx/colour.h>
#endif #endif
#include "ass_entry.h" #include "ass_entry.h"
/// DOCME /// DOCME
/// @class AssColor /// @class AssColor
/// @brief DOCME /// @brief DOCME
/// ///
/// DOCME /// DOCME
class AssColor { struct AssColor {
public: int r; ///< Red component
int g; ///< Green component
/// DOCME int b; ///< Blue component
int r; // Red component int a; ///< Alpha component
/// DOCME
int g; // Green component
/// DOCME
int b; // Blue component
/// DOCME
int a; // Alpha component
AssColor(); AssColor();
AssColor(wxColour &color); AssColor(int r, int g, int b, int a = 0);
AssColor(const wxColour &color);
bool operator==(AssColor &col) const; bool operator==(const AssColor &col) const;
bool operator!=(AssColor &col) const; bool operator!=(const AssColor &col) const;
wxColor GetWXColor(); // Return as a wxColor wxColor GetWXColor(); // Return as a wxColor
void SetWXColor(const wxColor &color); // Sets from a wxColor void SetWXColor(const wxColor &color); // Sets from a wxColor
void Parse(const wxString value); // Parse SSA or ASS-style color void Parse(const wxString value); // Parse SSA or ASS-style color
wxString GetASSFormatted(bool alpha,bool stripped=false,bool isStyle=false); // Gets color formated in ASS format wxString GetASSFormatted(bool alpha,bool stripped=false,bool isStyle=false) const; // Gets color formated in ASS format
wxString GetSSAFormatted(); wxString GetSSAFormatted() const;
}; };
/// DOCME /// DOCME
/// @class AssStyle /// @class AssStyle
/// @brief DOCME /// @brief DOCME
@ -88,7 +72,6 @@ public:
/// DOCME /// DOCME
class AssStyle : public AssEntry { class AssStyle : public AssEntry {
public: public:
/// DOCME /// DOCME
wxString name; wxString name;
@ -161,20 +144,21 @@ public:
/// @brief DOCME /// @brief DOCME
/// ///
ASS_EntryType GetType() { return ENTRY_STYLE; } ASS_EntryType GetType() const { return ENTRY_STYLE; }
bool Parse(wxString data,int version=1); // Parses raw ASS/SSA data into everything else bool Parse(wxString data,int version=1); // Parses raw ASS/SSA data into everything else
void UpdateData(); // Updates raw data void UpdateData(); // Updates raw data
wxString GetSSAText(); // Retrieves SSA-formatted style wxString GetSSAText() const; // Retrieves SSA-formatted style
wxString GetMarginString(int which); // Returns the margin value as a string (0 = left, 1 = right, 2 = vertical/top, 3 = bottom) wxString GetMarginString(int which) const; // Returns the margin value as a string (0 = left, 1 = right, 2 = vertical/top, 3 = bottom)
void SetMarginString(const wxString value,int which); // Sets margin value from a string (0 = left, 1 = right, 2 = vertical/top, 3 = bottom) void SetMarginString(const wxString value,int which); // Sets margin value from a string (0 = left, 1 = right, 2 = vertical/top, 3 = bottom)
static void GetEncodings(wxArrayString &encodingStrings); static void GetEncodings(wxArrayString &encodingStrings);
AssEntry *Clone() const; AssEntry *Clone() const;
bool IsEqualTo(AssStyle *style); bool IsEqualTo(AssStyle *style) const;
AssStyle(); AssStyle();
AssStyle(AssStyle const&);
AssStyle(wxString data,int version=1); AssStyle(wxString data,int version=1);
~AssStyle(); ~AssStyle();
}; };

View file

@ -171,7 +171,7 @@ void AssTime::SetMS (int _ms) {
/// @param msPrecision /// @param msPrecision
/// @return /// @return
/// ///
wxString AssTime::GetASSFormated (bool msPrecision) { wxString AssTime::GetASSFormated (bool msPrecision) const {
int h,m,s,ms; int h,m,s,ms;
int _ms = time; int _ms = time;

View file

@ -76,7 +76,7 @@ public:
void SetMS(int ms); // Sets values to miliseconds void SetMS(int ms); // Sets values to miliseconds
void ParseASS(const wxString text); // Sets value to text-form time, in ASS format void ParseASS(const wxString text); // Sets value to text-form time, in ASS format
void ParseSRT(const wxString text); // Sets value to text-form time, in SRT format void ParseSRT(const wxString text); // Sets value to text-form time, in SRT format
wxString GetASSFormated(bool ms=false); // Returns the ASS representation of time wxString GetASSFormated(bool ms=false) const; // Returns the ASS representation of time
wxString GetSRTFormated(); // Returns the SRT representation of time wxString GetSRTFormated(); // Returns the SRT representation of time
}; };