Skip to content

Commit 2bf7293

Browse files
committed
Added suggested MCStringLineEndingStyle enum and MCStringNormalizeLineEndings function. Did not remove the other added code yet nor convert other line ending conversion calls to the new version yet.
1 parent 2e9066b commit 2bf7293

File tree

5 files changed

+83
-28
lines changed

5 files changed

+83
-28
lines changed

engine/src/dispatch.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -673,10 +673,14 @@ static MCStack* script_only_stack_from_bytes(uint8_t *p_bytes,
673673
MCStringEncoding p_encoding)
674674
{
675675
MCAutoStringRef t_raw_script_string, t_LC_script_string;
676+
MCStringLineEndingStyle t_line_encoding_style;
677+
676678
if (!MCStringCreateWithBytes(p_bytes, p_size, p_encoding, false,
677679
&t_raw_script_string) ||
678-
!MCStringConvertLineEndingsToLiveCode(*t_raw_script_string,
679-
&t_LC_script_string))
680+
!MCStringNormalizeLineEndings(*t_raw_script_string,
681+
kMCStringLineEndingStyleLF,
682+
&t_LC_script_string,
683+
&t_line_encoding_style))
680684
{
681685
return nullptr;
682686
}
@@ -752,13 +756,7 @@ static MCStack* script_only_stack_from_bytes(uint8_t *p_bytes,
752756
t_stack -> setname(*t_script_name);
753757

754758
// Save line endings from raw script string to restore when saving file.
755-
t_stack -> setuseLFlineendings(MCStringContains(*t_raw_script_string,
756-
MCSTR("\n"), kMCStringOptionCompareExact));
757-
t_stack -> setuseCRlineendings(MCStringContains(*t_raw_script_string,
758-
MCSTR("\r"), kMCStringOptionCompareExact));
759-
// If neither line ending character found, default to LF.
760-
if (!(t_stack->getuseLFlineendings() || t_stack->getuseCRlineendings()))
761-
t_stack -> setuseLFlineendings(true);
759+
t_stack -> setlineencodingstyle(t_line_encoding_style);
762760

763761
return t_stack;
764762
}
@@ -1128,6 +1126,7 @@ IO_stat MCDispatch::dosavescriptonlystack(MCStack *sptr, const MCStringRef p_fna
11281126

11291127
// Compute the body of the script file.
11301128
MCAutoStringRef t_converted;
1129+
//MCStringLineEndingStyle t_line_encoding_style;
11311130

11321131
// MW-2014-10-24: [[ Bug 13791 ]] We need to post-process the generated string on some
11331132
// platforms for line-ending conversion so temporarily need a stringref - hence we
@@ -1141,10 +1140,8 @@ IO_stat MCDispatch::dosavescriptonlystack(MCStack *sptr, const MCStringRef p_fna
11411140
// Write out the standard script stack header, and then the script itself
11421141
MCStringFormat(&t_script_body, "script \"%@\"\n%@", sptr -> getname(), sptr->_getscript());
11431142

1144-
MCStringConvertLineEndingsFromLiveCode(*t_script_body,
1145-
sptr -> getuseLFlineendings(),
1146-
sptr -> getuseCRlineendings(),
1147-
&t_converted);
1143+
MCStringNormalizeLineEndings(*t_script_body, sptr -> getlineencodingstyle(),
1144+
&t_converted, nullptr);
11481145
}
11491146

11501147
// Open the output stream.

engine/src/stack.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ MCStack::MCStack()
304304
m_is_script_only = false;
305305

306306
// BWM-2017-08-16: [[ Bug 17810 ]] Script-only-stack line endings default to LF.
307-
m_use_LF_line_endings = true;
308-
m_use_CR_line_endings = false;
307+
m_line_encoding_style = kMCStringLineEndingStyleLF;
309308

310309
// IM-2014-05-27: [[ Bug 12321 ]] No fonts to purge yet
311310
m_purge_fonts = false;
@@ -2050,14 +2049,9 @@ void MCStack::setasscriptonly(MCStringRef p_script)
20502049

20512050
// BWM-2017-08-16: [[ Bug 17810 ]] Retain line endings used when importing a
20522051
// script-only-stack.
2053-
void MCStack::setuseLFlineendings(bool p_value)
2052+
void MCStack::setlineencodingstyle(MCStringLineEndingStyle p_line_encoding_style)
20542053
{
2055-
m_use_LF_line_endings = p_value;
2056-
}
2057-
2058-
void MCStack::setuseCRlineendings(bool p_value)
2059-
{
2060-
m_use_CR_line_endings = p_value;
2054+
m_line_encoding_style = p_line_encoding_style;
20612055
}
20622056

20632057

engine/src/stack.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ class MCStack : public MCObject, public MCMixinObjectHandle<MCStack>
302302
bool m_is_script_only : 1;
303303

304304
// BWM-2017-08-16: [[ Bug 17810 ]] Line endings for imported script-only-stack.
305-
bool m_use_LF_line_endings : 1;
306-
bool m_use_CR_line_endings : 1;
305+
MCStringLineEndingStyle m_line_encoding_style;
307306

308307
bool m_is_ide_stack : 1;
309308

@@ -952,10 +951,8 @@ class MCStack : public MCObject, public MCMixinObjectHandle<MCStack>
952951
void setasscriptonly(MCStringRef p_script);
953952

954953
// BWM-2017-08-16: [[ Bug 17810 ]] Get/set line endings for imported script-only-stack.
955-
bool getuseLFlineendings(void) const { return m_use_LF_line_endings; }
956-
bool getuseCRlineendings(void) const { return m_use_CR_line_endings; }
957-
void setuseLFlineendings(bool p_value);
958-
void setuseCRlineendings(bool p_value);
954+
MCStringLineEndingStyle getlineencodingstyle(void) const { return m_line_encoding_style; }
955+
void setlineencodingstyle(MCStringLineEndingStyle p_line_encoding_style);
959956

960957
inline bool getextendedstate(uint4 flag) const
961958
{

libfoundation/include/foundation.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,19 @@ MC_DLLEXPORT bool MCStringNormalizedCopyNFKD(MCStringRef, MCStringRef&);
27232723
MC_DLLEXPORT bool MCStringSetNumericValue(MCStringRef self, double p_value);
27242724
MC_DLLEXPORT bool MCStringGetNumericValue(MCStringRef self, double &r_value);
27252725

2726+
enum MCStringLineEndingStyle
2727+
{
2728+
kMCStringLineEndingStyleLF,
2729+
kMCStringLineEndingStyleCR,
2730+
kMCStringLineEndingStyleCRLF
2731+
};
2732+
2733+
MC_DLLEXPORT bool MCStringNormalizeLineEndings(
2734+
MCStringRef p_input,
2735+
MCStringLineEndingStyle p_to_style,
2736+
MCStringRef& r_output,
2737+
MCStringLineEndingStyle* r_original_style);
2738+
27262739
////////////////////////////////////////////////////////////////////////////////
27272740
//
27282741
// DATA DEFINITIONS

libfoundation/src/foundation-string.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6963,6 +6963,60 @@ bool MCStringGetNumericValue(MCStringRef self, double &r_value)
69636963
return false;
69646964
}
69656965

6966+
MC_DLLEXPORT
6967+
bool MCStringNormalizeLineEndings(MCStringRef p_input,
6968+
MCStringLineEndingStyle p_to_style,
6969+
MCStringRef& r_output,
6970+
MCStringLineEndingStyle* r_original_style)
6971+
{
6972+
uindex_t t_firstLF = 0;
6973+
uindex_t t_firstCR = 0;
6974+
MCStringLineEndingStyle t_original_style = kMCStringLineEndingStyleLF;
6975+
MCStringRef t_mutable_input;
6976+
6977+
if (MCStringFirstIndexOfChar(p_input, '\r', 0, kMCStringOptionCompareExact, t_firstCR))
6978+
{
6979+
if (MCStringFirstIndexOfChar(p_input, '\n', 0, kMCStringOptionCompareExact, t_firstLF))
6980+
{
6981+
if ((t_firstCR + 1 == t_firstLF) || (t_firstLF + 1 == t_firstCR))
6982+
t_original_style = kMCStringLineEndingStyleCRLF;
6983+
else if (t_firstCR < t_firstLF)
6984+
t_original_style = kMCStringLineEndingStyleCR;
6985+
}
6986+
else
6987+
t_original_style = kMCStringLineEndingStyleCR;
6988+
}
6989+
6990+
// normalize input to LF line endings first
6991+
/* UNCHECKED */ MCStringMutableCopy(p_input, t_mutable_input);
6992+
/* UNCHECKED */ MCStringFindAndReplace(t_mutable_input, MCSTR("\r\n"),
6993+
MCSTR("\n\r"), kMCStringOptionCompareExact);
6994+
/* UNCHECKED */ MCStringFindAndReplace(t_mutable_input, MCSTR("\n\r"),
6995+
MCSTR("\n"), kMCStringOptionCompareExact);
6996+
/* UNCHECKED */ MCStringFindAndReplace(t_mutable_input, MCSTR("\r"),
6997+
MCSTR("\n"), kMCStringOptionCompareExact);
6998+
6999+
// AL-2014-07-21: [[ Bug 12162 ]] Convert PS to LF, and LS to VT on text import.
7000+
/* UNCHECKED */ MCStringFindAndReplaceChar (t_mutable_input,
7001+
(const codepoint_t)0x2028, (const codepoint_t)0x0B,
7002+
kMCStringOptionCompareExact);
7003+
/* UNCHECKED */ MCStringFindAndReplaceChar (t_mutable_input,
7004+
(const codepoint_t)0x2029, (const codepoint_t)0x0A,
7005+
kMCStringOptionCompareExact);
7006+
7007+
// now convert the line endings to the proper version
7008+
if (p_to_style == kMCStringLineEndingStyleCR)
7009+
/* UNCHECKED */ MCStringFindAndReplace(t_mutable_input, MCSTR("\n"),
7010+
MCSTR("\r"), kMCStringOptionCompareExact);
7011+
else if (p_to_style == kMCStringLineEndingStyleCRLF)
7012+
/* UNCHECKED */ MCStringFindAndReplace(t_mutable_input, MCSTR("\n"),
7013+
MCSTR("\r\n"), kMCStringOptionCompareExact);
7014+
7015+
if (r_original_style != nullptr) *r_original_style = t_original_style;
7016+
/* UNCHECKED */ MCStringCopyAndRelease(t_mutable_input, r_output);
7017+
return true;
7018+
}
7019+
69667020
static void __MCStringCheck(MCStringRef self)
69677021
{
69687022
__MCAssertIsString(self);

0 commit comments

Comments
 (0)