PageRenderTime 247ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/4.1_LWIP/NETMF_4_1_Modified_Files/CLR/Libraries/CoreLib/corlib_native_System_String.cpp

#
C++ | 1077 lines | 775 code | 283 blank | 19 comment | 106 complexity | ab917ab2cccea02dc7e4721b65379fea MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4. #include "CorLib.h"
  5. #pragma arm section code = "SectionForAcceleration"
  6. static const CLR_UINT16 c_WhiteSpaces[] =
  7. {
  8. 0x0009,
  9. 0x000A,
  10. 0x000B,
  11. 0x000C,
  12. 0x000D,
  13. 0x0020,
  14. 0x00A0,
  15. 0x2000,
  16. 0x2001,
  17. 0x2002,
  18. 0x2003,
  19. 0x2004,
  20. 0x2005,
  21. 0x2006,
  22. 0x2007,
  23. 0x2008,
  24. 0x2009,
  25. 0x200A,
  26. 0x200B,
  27. 0x3000,
  28. 0xFEFF,
  29. };
  30. //--//
  31. HRESULT Library_corlib_native_System_String::get_Chars___CHAR__I4( CLR_RT_StackFrame& stack )
  32. {
  33. NATIVE_PROFILE_CLR_CORE();
  34. TINYCLR_HEADER();
  35. LPCSTR szText;
  36. CLR_RT_UnicodeHelper uh;
  37. CLR_UINT16 buf[ 3 ];
  38. int len;
  39. int index;
  40. szText = stack.Arg0().RecoverString(); FAULT_ON_NULL(szText);
  41. uh.SetInputUTF8( szText );
  42. len = uh.CountNumberOfCharacters() ; if(len < 0 ) TINYCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
  43. index = stack.Arg1().NumericByRef().s4; if(index < 0 || index >= len) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
  44. uh.m_outputUTF16 = buf;
  45. uh.m_outputUTF16_size = MAXSTRLEN(buf);
  46. //
  47. // First move to the character, then read it.
  48. //
  49. uh.ConvertFromUTF8( index, true );
  50. uh.ConvertFromUTF8( 1 , false );
  51. stack.SetResult( buf[ 0 ], DATATYPE_CHAR );
  52. TINYCLR_NOCLEANUP();
  53. }
  54. HRESULT Library_corlib_native_System_String::ToCharArray___SZARRAY_CHAR( CLR_RT_StackFrame& stack )
  55. {
  56. NATIVE_PROFILE_CLR_CORE();
  57. TINYCLR_HEADER();
  58. TINYCLR_SET_AND_LEAVE(ToCharArray( stack, 0, -1 ));
  59. TINYCLR_NOCLEANUP();
  60. }
  61. HRESULT Library_corlib_native_System_String::ToCharArray___SZARRAY_CHAR__I4__I4( CLR_RT_StackFrame& stack )
  62. {
  63. NATIVE_PROFILE_CLR_CORE();
  64. TINYCLR_HEADER();
  65. TINYCLR_SET_AND_LEAVE(ToCharArray( stack, stack.Arg1().NumericByRef().s4, stack.Arg2().NumericByRef().s4 ));
  66. TINYCLR_NOCLEANUP();
  67. }
  68. HRESULT Library_corlib_native_System_String::get_Length___I4( CLR_RT_StackFrame& stack )
  69. {
  70. NATIVE_PROFILE_CLR_CORE();
  71. TINYCLR_HEADER();
  72. LPCSTR szText = stack.Arg0().RecoverString(); FAULT_ON_NULL(szText);
  73. CLR_RT_UnicodeHelper uh; uh.SetInputUTF8( szText );
  74. stack.SetResult_I4( uh.CountNumberOfCharacters() );
  75. TINYCLR_NOCLEANUP();
  76. }
  77. HRESULT Library_corlib_native_System_String::Split___SZARRAY_STRING__SZARRAY_CHAR( CLR_RT_StackFrame& stack )
  78. {
  79. NATIVE_PROFILE_CLR_CORE();
  80. TINYCLR_HEADER();
  81. TINYCLR_SET_AND_LEAVE(Split( stack, stack.Arg1(), 0x7FFFFFFF )); /// Sending INT_MAX instead of -1.
  82. TINYCLR_NOCLEANUP();
  83. }
  84. HRESULT Library_corlib_native_System_String::Split___SZARRAY_STRING__SZARRAY_CHAR__I4( CLR_RT_StackFrame& stack )
  85. {
  86. NATIVE_PROFILE_CLR_CORE();
  87. TINYCLR_HEADER();
  88. TINYCLR_SET_AND_LEAVE(Split( stack, stack.Arg1(), stack.Arg2().NumericByRef().s4 ));
  89. TINYCLR_NOCLEANUP();
  90. }
  91. HRESULT Library_corlib_native_System_String::Substring___STRING__I4( CLR_RT_StackFrame& stack )
  92. {
  93. NATIVE_PROFILE_CLR_CORE();
  94. TINYCLR_HEADER();
  95. TINYCLR_SET_AND_LEAVE(Substring( stack, stack.Arg1().NumericByRef().s4, -1 ));
  96. TINYCLR_NOCLEANUP();
  97. }
  98. HRESULT Library_corlib_native_System_String::Substring___STRING__I4__I4( CLR_RT_StackFrame& stack )
  99. {
  100. NATIVE_PROFILE_CLR_CORE();
  101. TINYCLR_HEADER();
  102. CLR_INT32 length = stack.Arg2().NumericByRef().s4;
  103. if(length < 0) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
  104. TINYCLR_SET_AND_LEAVE(Substring( stack, stack.Arg1().NumericByRef().s4, length ));
  105. TINYCLR_NOCLEANUP();
  106. }
  107. HRESULT Library_corlib_native_System_String::Trim___STRING__SZARRAY_CHAR( CLR_RT_StackFrame& stack )
  108. {
  109. NATIVE_PROFILE_CLR_CORE();
  110. TINYCLR_HEADER();
  111. TINYCLR_SET_AND_LEAVE(Trim( stack, stack.Arg1().DereferenceArray(), true, true ));
  112. TINYCLR_NOCLEANUP();
  113. }
  114. HRESULT Library_corlib_native_System_String::TrimStart___STRING__SZARRAY_CHAR( CLR_RT_StackFrame& stack )
  115. {
  116. NATIVE_PROFILE_CLR_CORE();
  117. TINYCLR_HEADER();
  118. TINYCLR_SET_AND_LEAVE(Trim( stack, stack.Arg1().DereferenceArray(), true, false ));
  119. TINYCLR_NOCLEANUP();
  120. }
  121. HRESULT Library_corlib_native_System_String::TrimEnd___STRING__SZARRAY_CHAR( CLR_RT_StackFrame& stack )
  122. {
  123. NATIVE_PROFILE_CLR_CORE();
  124. TINYCLR_HEADER();
  125. TINYCLR_SET_AND_LEAVE(Trim( stack, stack.Arg1().DereferenceArray(), false, true ));
  126. TINYCLR_NOCLEANUP();
  127. }
  128. HRESULT Library_corlib_native_System_String::_ctor___VOID__SZARRAY_CHAR__I4__I4( CLR_RT_StackFrame& stack )
  129. {
  130. NATIVE_PROFILE_CLR_CORE();
  131. TINYCLR_HEADER();
  132. TINYCLR_SET_AND_LEAVE(FromCharArray( stack, stack.Arg2().NumericByRef().s4, stack.Arg3().NumericByRef().s4 ));
  133. TINYCLR_NOCLEANUP();
  134. }
  135. HRESULT Library_corlib_native_System_String::_ctor___VOID__SZARRAY_CHAR( CLR_RT_StackFrame& stack )
  136. {
  137. NATIVE_PROFILE_CLR_CORE();
  138. TINYCLR_HEADER();
  139. TINYCLR_SET_AND_LEAVE(FromCharArray( stack, 0, -1 ));
  140. TINYCLR_NOCLEANUP();
  141. }
  142. HRESULT Library_corlib_native_System_String::_ctor___VOID__CHAR__I4( CLR_RT_StackFrame& stack )
  143. {
  144. NATIVE_PROFILE_CLR_CORE();
  145. TINYCLR_HEADER();
  146. CLR_UINT16 ch = stack.Arg1().NumericByRef().u2;
  147. int len = stack.Arg2().NumericByRef().s4; if(len < 0) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
  148. {
  149. CLR_RT_HeapBlock tmp; tmp.SetObjectReference( NULL );
  150. CLR_RT_ProtectFromGC gc( tmp );
  151. TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( tmp, len, g_CLR_RT_WellKnownTypes.m_Char ));
  152. {
  153. CLR_RT_HeapBlock_Array* tmpArray = tmp.DereferenceArray();
  154. CLR_UINT16* pTmp = (CLR_UINT16*)tmpArray->GetFirstElement();
  155. CLR_UINT16* p;
  156. p = pTmp; while(len--) *p++ = ch;
  157. TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_String::CreateInstance( stack.Arg0(), pTmp, tmpArray->m_numOfElements ));
  158. }
  159. }
  160. TINYCLR_NOCLEANUP();
  161. }
  162. HRESULT Library_corlib_native_System_String::CompareTo___I4__OBJECT( CLR_RT_StackFrame& stack )
  163. {
  164. NATIVE_PROFILE_CLR_CORE();
  165. TINYCLR_HEADER();
  166. TINYCLR_SET_AND_LEAVE(Library_corlib_native_System_String::CompareTo___I4__STRING( stack ));
  167. TINYCLR_NOCLEANUP();
  168. }
  169. HRESULT Library_corlib_native_System_String::CompareTo___I4__STRING( CLR_RT_StackFrame& stack )
  170. {
  171. NATIVE_PROFILE_CLR_CORE();
  172. TINYCLR_HEADER();
  173. CLR_RT_HeapBlock& pThis = stack.Arg0(); // String references are special, they don't point to an object, they are the object. So use stack.Arg0() instead of stack.This()
  174. CLR_RT_HeapBlock& pArg = stack.Arg1();
  175. stack.SetResult_I4( CLR_RT_HeapBlock::Compare_Unsigned_Values( pThis, pArg ) );
  176. TINYCLR_NOCLEANUP_NOLABEL();
  177. }
  178. HRESULT Library_corlib_native_System_String::IndexOf___I4__CHAR( CLR_RT_StackFrame& stack )
  179. {
  180. NATIVE_PROFILE_CLR_CORE();
  181. TINYCLR_HEADER();
  182. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__SingleChar ));
  183. TINYCLR_NOCLEANUP();
  184. }
  185. HRESULT Library_corlib_native_System_String::IndexOf___I4__CHAR__I4( CLR_RT_StackFrame& stack )
  186. {
  187. NATIVE_PROFILE_CLR_CORE();
  188. TINYCLR_HEADER();
  189. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__SingleChar | c_IndexOf__StartIndex ));
  190. TINYCLR_NOCLEANUP();
  191. }
  192. HRESULT Library_corlib_native_System_String::IndexOf___I4__CHAR__I4__I4( CLR_RT_StackFrame& stack )
  193. {
  194. NATIVE_PROFILE_CLR_CORE();
  195. TINYCLR_HEADER();
  196. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__SingleChar | c_IndexOf__StartIndex | c_IndexOf__Count ));
  197. TINYCLR_NOCLEANUP();
  198. }
  199. HRESULT Library_corlib_native_System_String::IndexOfAny___I4__SZARRAY_CHAR( CLR_RT_StackFrame& stack )
  200. {
  201. NATIVE_PROFILE_CLR_CORE();
  202. TINYCLR_HEADER();
  203. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__MultipleChars ));
  204. TINYCLR_NOCLEANUP();
  205. }
  206. HRESULT Library_corlib_native_System_String::IndexOfAny___I4__SZARRAY_CHAR__I4( CLR_RT_StackFrame& stack )
  207. {
  208. NATIVE_PROFILE_CLR_CORE();
  209. TINYCLR_HEADER();
  210. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__MultipleChars | c_IndexOf__StartIndex ));
  211. TINYCLR_NOCLEANUP();
  212. }
  213. HRESULT Library_corlib_native_System_String::IndexOfAny___I4__SZARRAY_CHAR__I4__I4( CLR_RT_StackFrame& stack )
  214. {
  215. NATIVE_PROFILE_CLR_CORE();
  216. TINYCLR_HEADER();
  217. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__MultipleChars | c_IndexOf__StartIndex | c_IndexOf__Count ));
  218. TINYCLR_NOCLEANUP();
  219. }
  220. HRESULT Library_corlib_native_System_String::IndexOf___I4__STRING( CLR_RT_StackFrame& stack )
  221. {
  222. NATIVE_PROFILE_CLR_CORE();
  223. TINYCLR_HEADER();
  224. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__String ));
  225. TINYCLR_NOCLEANUP();
  226. }
  227. HRESULT Library_corlib_native_System_String::IndexOf___I4__STRING__I4( CLR_RT_StackFrame& stack )
  228. {
  229. NATIVE_PROFILE_CLR_CORE();
  230. TINYCLR_HEADER();
  231. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__String | c_IndexOf__StartIndex ));
  232. TINYCLR_NOCLEANUP();
  233. }
  234. HRESULT Library_corlib_native_System_String::IndexOf___I4__STRING__I4__I4( CLR_RT_StackFrame& stack )
  235. {
  236. NATIVE_PROFILE_CLR_CORE();
  237. TINYCLR_HEADER();
  238. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__String | c_IndexOf__StartIndex | c_IndexOf__Count ));
  239. TINYCLR_NOCLEANUP();
  240. }
  241. HRESULT Library_corlib_native_System_String::LastIndexOf___I4__CHAR( CLR_RT_StackFrame& stack )
  242. {
  243. NATIVE_PROFILE_CLR_CORE();
  244. TINYCLR_HEADER();
  245. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__SingleChar | c_IndexOf__Last ));
  246. TINYCLR_NOCLEANUP();
  247. }
  248. HRESULT Library_corlib_native_System_String::LastIndexOf___I4__CHAR__I4( CLR_RT_StackFrame& stack )
  249. {
  250. NATIVE_PROFILE_CLR_CORE();
  251. TINYCLR_HEADER();
  252. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__SingleChar | c_IndexOf__Last | c_IndexOf__StartIndex ));
  253. TINYCLR_NOCLEANUP();
  254. }
  255. HRESULT Library_corlib_native_System_String::LastIndexOf___I4__CHAR__I4__I4( CLR_RT_StackFrame& stack )
  256. {
  257. NATIVE_PROFILE_CLR_CORE();
  258. TINYCLR_HEADER();
  259. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__SingleChar | c_IndexOf__Last | c_IndexOf__StartIndex | c_IndexOf__Count ));
  260. TINYCLR_NOCLEANUP();
  261. }
  262. HRESULT Library_corlib_native_System_String::LastIndexOfAny___I4__SZARRAY_CHAR( CLR_RT_StackFrame& stack )
  263. {
  264. NATIVE_PROFILE_CLR_CORE();
  265. TINYCLR_HEADER();
  266. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__MultipleChars | c_IndexOf__Last ));
  267. TINYCLR_NOCLEANUP();
  268. }
  269. HRESULT Library_corlib_native_System_String::LastIndexOfAny___I4__SZARRAY_CHAR__I4( CLR_RT_StackFrame& stack )
  270. {
  271. NATIVE_PROFILE_CLR_CORE();
  272. TINYCLR_HEADER();
  273. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__MultipleChars | c_IndexOf__Last | c_IndexOf__StartIndex ));
  274. TINYCLR_NOCLEANUP();
  275. }
  276. HRESULT Library_corlib_native_System_String::LastIndexOfAny___I4__SZARRAY_CHAR__I4__I4( CLR_RT_StackFrame& stack )
  277. {
  278. NATIVE_PROFILE_CLR_CORE();
  279. TINYCLR_HEADER();
  280. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__MultipleChars | c_IndexOf__Last | c_IndexOf__StartIndex | c_IndexOf__Count ));
  281. TINYCLR_NOCLEANUP();
  282. }
  283. HRESULT Library_corlib_native_System_String::LastIndexOf___I4__STRING( CLR_RT_StackFrame& stack )
  284. {
  285. NATIVE_PROFILE_CLR_CORE();
  286. TINYCLR_HEADER();
  287. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__String | c_IndexOf__Last ));
  288. TINYCLR_NOCLEANUP();
  289. }
  290. HRESULT Library_corlib_native_System_String::LastIndexOf___I4__STRING__I4( CLR_RT_StackFrame& stack )
  291. {
  292. NATIVE_PROFILE_CLR_CORE();
  293. TINYCLR_HEADER();
  294. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__String | c_IndexOf__Last | c_IndexOf__StartIndex ));
  295. TINYCLR_NOCLEANUP();
  296. }
  297. HRESULT Library_corlib_native_System_String::LastIndexOf___I4__STRING__I4__I4( CLR_RT_StackFrame& stack )
  298. {
  299. NATIVE_PROFILE_CLR_CORE();
  300. TINYCLR_HEADER();
  301. TINYCLR_SET_AND_LEAVE(IndexOf( stack, c_IndexOf__String | c_IndexOf__Last | c_IndexOf__StartIndex | c_IndexOf__Count ));
  302. TINYCLR_NOCLEANUP();
  303. }
  304. HRESULT Library_corlib_native_System_String::ToLower___STRING( CLR_RT_StackFrame& stack )
  305. {
  306. NATIVE_PROFILE_CLR_CORE();
  307. TINYCLR_HEADER();
  308. TINYCLR_SET_AND_LEAVE(ChangeCase( stack, false ));
  309. TINYCLR_NOCLEANUP();
  310. }
  311. HRESULT Library_corlib_native_System_String::ToUpper___STRING( CLR_RT_StackFrame& stack )
  312. {
  313. NATIVE_PROFILE_CLR_CORE();
  314. TINYCLR_HEADER();
  315. TINYCLR_SET_AND_LEAVE(ChangeCase( stack, true ));
  316. TINYCLR_NOCLEANUP();
  317. }
  318. HRESULT Library_corlib_native_System_String::Trim___STRING( CLR_RT_StackFrame& stack )
  319. {
  320. NATIVE_PROFILE_CLR_CORE();
  321. TINYCLR_HEADER();
  322. TINYCLR_SET_AND_LEAVE(Trim( stack, NULL, true, true ));
  323. TINYCLR_NOCLEANUP();
  324. }
  325. HRESULT Library_corlib_native_System_String::Equals___STATIC__BOOLEAN__STRING__STRING( CLR_RT_StackFrame& stack )
  326. {
  327. NATIVE_PROFILE_CLR_CORE();
  328. TINYCLR_HEADER();
  329. stack.SetResult_Boolean( CLR_RT_HeapBlock::Compare_Unsigned_Values( stack.Arg0(), stack.Arg1() ) == 0 );
  330. TINYCLR_NOCLEANUP_NOLABEL();
  331. }
  332. HRESULT Library_corlib_native_System_String::op_Equality___STATIC__BOOLEAN__STRING__STRING( CLR_RT_StackFrame& stack )
  333. {
  334. NATIVE_PROFILE_CLR_CORE();
  335. TINYCLR_HEADER();
  336. TINYCLR_SET_AND_LEAVE(Library_corlib_native_System_String::Equals___STATIC__BOOLEAN__STRING__STRING( stack ));
  337. TINYCLR_NOCLEANUP();
  338. }
  339. HRESULT Library_corlib_native_System_String::op_Inequality___STATIC__BOOLEAN__STRING__STRING( CLR_RT_StackFrame& stack )
  340. {
  341. NATIVE_PROFILE_CLR_CORE();
  342. TINYCLR_HEADER();
  343. TINYCLR_CHECK_HRESULT(Library_corlib_native_System_String::Equals___STATIC__BOOLEAN__STRING__STRING( stack ));
  344. stack.NegateResult();
  345. TINYCLR_NOCLEANUP();
  346. }
  347. HRESULT Library_corlib_native_System_String::Compare___STATIC__I4__STRING__STRING( CLR_RT_StackFrame& stack )
  348. {
  349. NATIVE_PROFILE_CLR_CORE();
  350. TINYCLR_HEADER();
  351. stack.SetResult_I4( CLR_RT_HeapBlock::Compare_Unsigned_Values( stack.Arg0(), stack.Arg1() ) );
  352. TINYCLR_NOCLEANUP_NOLABEL();
  353. }
  354. HRESULT Library_corlib_native_System_String::Concat___STATIC__STRING__STRING__STRING( CLR_RT_StackFrame& stack )
  355. {
  356. NATIVE_PROFILE_CLR_CORE();
  357. TINYCLR_HEADER();
  358. TINYCLR_SET_AND_LEAVE(Concat( stack, &stack.Arg0(), 2 ));
  359. TINYCLR_NOCLEANUP();
  360. }
  361. HRESULT Library_corlib_native_System_String::Concat___STATIC__STRING__STRING__STRING__STRING( CLR_RT_StackFrame& stack )
  362. {
  363. NATIVE_PROFILE_CLR_CORE();
  364. TINYCLR_HEADER();
  365. TINYCLR_SET_AND_LEAVE(Concat( stack, &stack.Arg0(), 3 ));
  366. TINYCLR_NOCLEANUP();
  367. }
  368. HRESULT Library_corlib_native_System_String::Concat___STATIC__STRING__STRING__STRING__STRING__STRING( CLR_RT_StackFrame& stack )
  369. {
  370. NATIVE_PROFILE_CLR_CORE();
  371. TINYCLR_HEADER();
  372. TINYCLR_SET_AND_LEAVE(Concat( stack, &stack.Arg0(), 4 ));
  373. TINYCLR_NOCLEANUP();
  374. }
  375. HRESULT Library_corlib_native_System_String::Concat___STATIC__STRING__SZARRAY_STRING( CLR_RT_StackFrame& stack )
  376. {
  377. NATIVE_PROFILE_CLR_CORE();
  378. TINYCLR_HEADER();
  379. CLR_RT_HeapBlock_Array* pArray = stack.Arg0().DereferenceArray(); FAULT_ON_NULL(pArray);
  380. TINYCLR_SET_AND_LEAVE(Concat( stack, (CLR_RT_HeapBlock*)pArray->GetFirstElement(), pArray->m_numOfElements ));
  381. TINYCLR_NOCLEANUP();
  382. }
  383. //--//
  384. HRESULT Library_corlib_native_System_String::FromCharArray( CLR_RT_StackFrame& stack, int startIndex, int length )
  385. {
  386. NATIVE_PROFILE_CLR_CORE();
  387. TINYCLR_HEADER();
  388. CLR_RT_HeapBlock_Array* array;
  389. CLR_UINT32 len;
  390. array = stack.Arg1().DereferenceArray(); if(!array) TINYCLR_SET_AND_LEAVE(S_OK);
  391. len = array->m_numOfElements ; if(!len ) TINYCLR_SET_AND_LEAVE(S_OK);
  392. if(length == -1) length = len - startIndex;
  393. if(CLR_RT_HeapBlock_Array::CheckRange( startIndex, length, len ) == false) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
  394. TINYCLR_SET_AND_LEAVE(CLR_RT_HeapBlock_String::CreateInstance( stack.Arg0(), (CLR_UINT16*)array->GetElement( startIndex ), length ));
  395. TINYCLR_NOCLEANUP();
  396. }
  397. HRESULT Library_corlib_native_System_String::ToCharArray( CLR_RT_StackFrame& stack, int startIndex, int length )
  398. {
  399. NATIVE_PROFILE_CLR_CORE();
  400. TINYCLR_HEADER();
  401. CLR_RT_HeapBlock_Array* array;
  402. TINYCLR_SET_AND_LEAVE(ConvertToCharArray( stack, stack.PushValueAndClear(), array, startIndex, length ));
  403. TINYCLR_NOCLEANUP();
  404. }
  405. HRESULT Library_corlib_native_System_String::IndexOf( CLR_RT_StackFrame& stack, int mode )
  406. {
  407. NATIVE_PROFILE_CLR_CORE();
  408. TINYCLR_HEADER();
  409. LPCSTR szText;
  410. int startIndex;
  411. int count;
  412. int pos;
  413. LPCSTR pString;
  414. const CLR_UINT16* pChars;
  415. int iChars = 0;
  416. CLR_RT_UnicodeHelper uh;
  417. int len;
  418. szText = stack.Arg0().RecoverString(); if(!szText) szText = "";
  419. pos = -1;
  420. pString = NULL;
  421. pChars = NULL;
  422. if(mode & c_IndexOf__SingleChar)
  423. {
  424. pChars = (CLR_UINT16*)&stack.Arg1().NumericByRefConst().u2;
  425. iChars = 1;
  426. }
  427. else if(mode & c_IndexOf__MultipleChars)
  428. {
  429. CLR_RT_HeapBlock_Array* array = stack.Arg1().DereferenceArray(); FAULT_ON_NULL(array);
  430. pChars = (const CLR_UINT16*)array->GetFirstElement();
  431. iChars = array->m_numOfElements;
  432. }
  433. else if(mode & c_IndexOf__String)
  434. {
  435. pString = stack.Arg1().RecoverString(); FAULT_ON_NULL(pString);
  436. }
  437. uh.SetInputUTF8( szText );
  438. len = uh.CountNumberOfCharacters();
  439. //--//
  440. if(mode & c_IndexOf__StartIndex)
  441. {
  442. startIndex = stack.Arg2().NumericByRefConst().s4;
  443. if(startIndex < 0 || startIndex > len) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
  444. }
  445. else
  446. {
  447. startIndex = 0;
  448. }
  449. if(mode & c_IndexOf__Count)
  450. {
  451. count = stack.Arg3().NumericByRefConst().s4;
  452. if(startIndex + count > len) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
  453. }
  454. else
  455. {
  456. count = len - startIndex;
  457. }
  458. //--//
  459. //
  460. // First move to the character, then read it.
  461. //
  462. if(uh.ConvertFromUTF8( startIndex, true ))
  463. {
  464. if(pString)
  465. {
  466. while(count-- > 0)
  467. {
  468. CLR_RT_UnicodeHelper uh1; uh1.SetInputUTF8( (LPCSTR)uh.m_inputUTF8 );
  469. CLR_RT_UnicodeHelper uh2; uh2.SetInputUTF8( pString );
  470. while(true)
  471. {
  472. CLR_UINT16 buf1[ 3 ];
  473. CLR_UINT16 buf2[ 3 ];
  474. uh1.m_outputUTF16 = buf1;
  475. uh1.m_outputUTF16_size = MAXSTRLEN(buf1);
  476. uh2.m_outputUTF16 = buf2;
  477. uh2.m_outputUTF16_size = MAXSTRLEN(buf2);
  478. if(uh2.ConvertFromUTF8( 1, false ) == false)
  479. {
  480. if((mode & c_IndexOf__Last) || (pos == -1))
  481. {
  482. pos = startIndex;
  483. }
  484. break;
  485. }
  486. if(uh1.ConvertFromUTF8( 1, false ) == false)
  487. {
  488. break;
  489. }
  490. if(buf1[ 0 ] != buf2[ 0 ])
  491. {
  492. break;
  493. }
  494. }
  495. if(uh.ConvertFromUTF8( 1, true ) == false)
  496. {
  497. break;
  498. }
  499. startIndex++;
  500. }
  501. }
  502. if(pChars)
  503. {
  504. while(count-- > 0)
  505. {
  506. CLR_UINT16 buf[ 3 ];
  507. uh.m_outputUTF16 = buf;
  508. uh.m_outputUTF16_size = MAXSTRLEN(buf);
  509. if(uh.ConvertFromUTF8( 1, false ) == false)
  510. {
  511. break;
  512. }
  513. for(int i=0; i<iChars; i++)
  514. {
  515. if(buf[ 0 ] == pChars[ i ])
  516. {
  517. if((mode & c_IndexOf__Last) || (pos == -1))
  518. {
  519. pos = startIndex;
  520. }
  521. break;
  522. }
  523. }
  524. startIndex++;
  525. }
  526. }
  527. }
  528. stack.SetResult_I4( pos );
  529. TINYCLR_NOCLEANUP();
  530. }
  531. HRESULT Library_corlib_native_System_String::ChangeCase( CLR_RT_StackFrame& stack, bool fToUpper )
  532. {
  533. NATIVE_PROFILE_CLR_CORE();
  534. TINYCLR_HEADER();
  535. CLR_RT_HeapBlock refTmp; refTmp.SetObjectReference( NULL );
  536. CLR_RT_ProtectFromGC gc( refTmp );
  537. CLR_RT_HeapBlock_Array* arrayTmp;
  538. CLR_UINT16* ptr;
  539. TINYCLR_CHECK_HRESULT(ConvertToCharArray( stack, refTmp, arrayTmp, 0, -1 ));
  540. ptr = (CLR_UINT16*)arrayTmp->GetFirstElement();
  541. for(CLR_UINT32 i=0; i<arrayTmp->m_numOfElements; i++)
  542. {
  543. CLR_UINT16 c = *ptr;
  544. if(fToUpper)
  545. {
  546. if(c >= 'a' && c <= 'z') c += 'A' - 'a';
  547. }
  548. else
  549. {
  550. if(c >= 'A' && c <= 'Z') c -= 'A' - 'a';
  551. }
  552. *ptr++ = c;
  553. }
  554. TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_String::CreateInstance( stack.PushValue(), (CLR_UINT16*)arrayTmp->GetFirstElement(), arrayTmp->m_numOfElements ));
  555. TINYCLR_NOCLEANUP();
  556. }
  557. HRESULT Library_corlib_native_System_String::Substring( CLR_RT_StackFrame& stack, int startIndex, int length )
  558. {
  559. NATIVE_PROFILE_CLR_CORE();
  560. TINYCLR_HEADER();
  561. CLR_RT_HeapBlock refTmp; refTmp.SetObjectReference( NULL );
  562. CLR_RT_ProtectFromGC gc( refTmp );
  563. CLR_RT_HeapBlock_Array* arrayTmp;
  564. TINYCLR_CHECK_HRESULT(ConvertToCharArray( stack, refTmp, arrayTmp, 0, -1 ));
  565. if(startIndex < 0 || startIndex > (int)arrayTmp->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
  566. if(length == -1)
  567. {
  568. length = arrayTmp->m_numOfElements - startIndex;
  569. }
  570. else
  571. {
  572. if(length < 0 || (startIndex + length) > (int)arrayTmp->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
  573. }
  574. TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_String::CreateInstance( stack.PushValue(), (CLR_UINT16*)arrayTmp->GetElement( startIndex ), length ));
  575. TINYCLR_NOCLEANUP();
  576. }
  577. HRESULT Library_corlib_native_System_String::Trim( CLR_RT_StackFrame& stack, CLR_RT_HeapBlock_Array* arrayTrimChars, bool fStart, bool fEnd )
  578. {
  579. NATIVE_PROFILE_CLR_CORE();
  580. TINYCLR_HEADER();
  581. CLR_RT_HeapBlock refTmp; refTmp.SetObjectReference( NULL );
  582. CLR_RT_ProtectFromGC gc( refTmp );
  583. CLR_RT_HeapBlock_Array* arrayTmp;
  584. CLR_UINT16* pSrcStart;
  585. CLR_UINT16* pSrcEnd;
  586. TINYCLR_CHECK_HRESULT(ConvertToCharArray( stack, refTmp, arrayTmp, 0, -1 ));
  587. pSrcStart = (CLR_UINT16*)arrayTmp->GetFirstElement();
  588. pSrcEnd = &pSrcStart[ arrayTmp->m_numOfElements ];
  589. const CLR_UINT16* pTrim;
  590. CLR_UINT32 iTrim;
  591. if(arrayTrimChars && arrayTrimChars->m_numOfElements)
  592. {
  593. pTrim = (const CLR_UINT16*)arrayTrimChars->GetFirstElement();
  594. iTrim = arrayTrimChars->m_numOfElements;
  595. }
  596. else
  597. {
  598. pTrim = c_WhiteSpaces;
  599. iTrim = ARRAYSIZE(c_WhiteSpaces);
  600. }
  601. //--//
  602. if(fStart)
  603. {
  604. while(pSrcStart < pSrcEnd)
  605. {
  606. const CLR_UINT16* p = pTrim;
  607. CLR_UINT32 i;
  608. for(i=0; i<iTrim; i++, p++)
  609. {
  610. if(*p == pSrcStart[ 0 ]) break;
  611. }
  612. if(i == iTrim) break;
  613. pSrcStart++;
  614. }
  615. }
  616. if(fEnd)
  617. {
  618. while(pSrcStart < pSrcEnd)
  619. {
  620. const CLR_UINT16* p = pTrim;
  621. CLR_UINT32 i;
  622. for(i=0; i<iTrim; i++, p++)
  623. {
  624. if(*p == pSrcEnd[ -1 ]) break;
  625. }
  626. if(i == iTrim) break;
  627. pSrcEnd--;
  628. }
  629. }
  630. TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_String::CreateInstance( stack.PushValue(), pSrcStart, (CLR_UINT32)(pSrcEnd - pSrcStart) ));
  631. TINYCLR_NOCLEANUP();
  632. }
  633. HRESULT Library_corlib_native_System_String::Split( CLR_RT_StackFrame& stack, CLR_RT_HeapBlock& chars, int maxStrings )
  634. {
  635. NATIVE_PROFILE_CLR_CORE();
  636. TINYCLR_HEADER();
  637. CLR_RT_HeapBlock_Array* arraySrc;
  638. CLR_RT_HeapBlock_Array* arrayChars;
  639. CLR_RT_HeapBlock_Array* arrayDst;
  640. const CLR_UINT16* pChars;
  641. CLR_UINT32 cChars;
  642. if (maxStrings < 0)
  643. {
  644. TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE)
  645. }
  646. else if (maxStrings == 0)
  647. {
  648. CLR_RT_HeapBlock& refTarget = stack.PushValue();
  649. TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( refTarget, 0, g_CLR_RT_WellKnownTypes.m_String ));
  650. arrayDst = refTarget.DereferenceArray();
  651. }
  652. else
  653. {
  654. arrayChars = chars.DereferenceArray();
  655. if(arrayChars != NULL && arrayChars->m_numOfElements > 0)
  656. {
  657. pChars = (CLR_UINT16*)arrayChars->GetFirstElement();
  658. cChars = arrayChars->m_numOfElements;
  659. }
  660. else
  661. {
  662. pChars = &c_WhiteSpaces[ 0 ];
  663. cChars = ARRAYSIZE(c_WhiteSpaces);
  664. }
  665. arrayDst = NULL;
  666. {
  667. CLR_RT_HeapBlock refSrc; refSrc.SetObjectReference( NULL );
  668. CLR_RT_ProtectFromGC gc( refSrc );
  669. TINYCLR_CHECK_HRESULT(ConvertToCharArray( stack, refSrc, arraySrc, 0, -1 ));
  670. for(int pass=0; pass<2; pass++)
  671. {
  672. CLR_UINT16* pSrcStart = (CLR_UINT16*)arraySrc->GetFirstElement();
  673. CLR_UINT16* pSrc = pSrcStart;
  674. int count = 0;
  675. bool fFound = false;
  676. bool fContinueSearching = true;
  677. for(CLR_UINT32 iSrc=0; iSrc <= arraySrc->m_numOfElements; iSrc++, pSrc++)
  678. {
  679. if(iSrc == arraySrc->m_numOfElements)
  680. {
  681. fFound = true;
  682. }
  683. else if ((count + 1) >= maxStrings)
  684. {
  685. fContinueSearching = false;
  686. }
  687. else if (fContinueSearching)
  688. {
  689. const CLR_UINT16* pCharsT = pChars;
  690. for(CLR_UINT32 iChars=0; iChars<cChars; iChars++)
  691. {
  692. if(pSrc[ 0 ] == *pCharsT++)
  693. {
  694. fFound = true;
  695. break;
  696. }
  697. }
  698. }
  699. if(fFound)
  700. {
  701. if(pass == 1)
  702. {
  703. CLR_RT_HeapBlock* str = (CLR_RT_HeapBlock*)arrayDst->GetElement( count );
  704. TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_String::CreateInstance( *str, pSrcStart, (CLR_UINT32)(pSrc - pSrcStart) ));
  705. pSrcStart = pSrc + 1;
  706. }
  707. count++;
  708. fFound = false;
  709. }
  710. }
  711. if(pass == 0)
  712. {
  713. CLR_RT_HeapBlock& refTarget = stack.PushValue();
  714. TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( refTarget, count, g_CLR_RT_WellKnownTypes.m_String ));
  715. arrayDst = refTarget.DereferenceArray();
  716. }
  717. }
  718. }
  719. }
  720. TINYCLR_NOCLEANUP();
  721. }
  722. //--//
  723. HRESULT Library_corlib_native_System_String::Concat( CLR_RT_StackFrame& stack, CLR_RT_HeapBlock* array, int num )
  724. {
  725. NATIVE_PROFILE_CLR_CORE();
  726. TINYCLR_HEADER();
  727. CLR_RT_HeapBlock* ptrSrc;
  728. LPCSTR szTextSrc;
  729. LPSTR szTextDst = NULL;
  730. CLR_UINT32 totLen;
  731. CLR_UINT32 len;
  732. totLen = 0;
  733. for(int i=0; i<2; i++)
  734. {
  735. ptrSrc = array;
  736. for(int iStr=0; iStr<num; iStr++)
  737. {
  738. _ASSERTE(ptrSrc->DataType() == DATATYPE_OBJECT);
  739. _ASSERTE(FIMPLIES(ptrSrc->Dereference(), ptrSrc->Dereference()->DataType() == DATATYPE_STRING));
  740. szTextSrc = ptrSrc->RecoverString();
  741. if(szTextSrc)
  742. {
  743. len = (CLR_UINT32)hal_strlen_s( szTextSrc );
  744. if(i==0)
  745. {
  746. totLen += len;
  747. }
  748. else
  749. {
  750. memcpy( szTextDst, szTextSrc, len );
  751. szTextDst += len;
  752. }
  753. }
  754. ptrSrc++;
  755. }
  756. if(i==0)
  757. {
  758. //push return value
  759. CLR_RT_HeapBlock& blkResult = stack.PushValue();
  760. CLR_RT_HeapBlock_String* str = CLR_RT_HeapBlock_String::CreateInstance( blkResult, totLen ); CHECK_ALLOCATION(str);
  761. szTextDst = (LPSTR)str->StringText();
  762. szTextDst[ totLen ] = 0;
  763. }
  764. }
  765. TINYCLR_NOCLEANUP();
  766. }
  767. HRESULT Library_corlib_native_System_String::ConvertToCharArray( LPCSTR szText, CLR_RT_HeapBlock& ref, CLR_RT_HeapBlock_Array*& array, int startIndex, int length )
  768. {
  769. NATIVE_PROFILE_CLR_CORE();
  770. TINYCLR_HEADER();
  771. CLR_RT_UnicodeHelper uh;
  772. int totLength;
  773. FAULT_ON_NULL(szText);
  774. uh.SetInputUTF8( szText );
  775. totLength = uh.CountNumberOfCharacters(); if(totLength < 0) TINYCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
  776. if(length == -1) length = totLength;
  777. if(CLR_RT_HeapBlock_Array::CheckRange( startIndex, length, totLength ) == false) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
  778. TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( ref, length, g_CLR_RT_WellKnownTypes.m_Char ));
  779. array = ref.DereferenceArray();
  780. uh.m_outputUTF16 = (CLR_UINT16*)array->GetFirstElement();
  781. uh.m_outputUTF16_size = array->m_numOfElements;
  782. //
  783. // First move to the character, then read it.
  784. //
  785. uh.ConvertFromUTF8( startIndex , true );
  786. uh.ConvertFromUTF8( array->m_numOfElements, false );
  787. TINYCLR_NOCLEANUP();
  788. }
  789. HRESULT Library_corlib_native_System_String::ConvertToCharArray( CLR_RT_StackFrame& stack, CLR_RT_HeapBlock& ref, CLR_RT_HeapBlock_Array*& array, int startIndex, int length )
  790. {
  791. NATIVE_PROFILE_CLR_CORE();
  792. return ConvertToCharArray( stack.Arg0().RecoverString(), ref, array, startIndex, length );
  793. }
  794. #pragma arm section code