|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.vishia.java2C.test.TestString
public class TestString
This class contains some examples to test String functionality.
Situation for String preparation in C:
A String preparation in C in embedded applications is often necessary in situations of error reporting
or output of states. The typically choice is usage of sprintf(buffer, "text-format", arguments, ...).
This approach have some risks which may produce errors in runtime:
buffer is less, an overflow isn't detect. An overflow may occur, if a value is unexpected,
and the numbering formating produce a less longer output. char*-Arguments are critically too,
because there length may be rate faulty because that is depending of outer code.
char* is used as argument, and the pointer is faulty, an unexpected buffer overflow
may be produced in the sprintf.
sprintf(...) in C should implement carefully, it is sensitive.
String result = "value=" + value. This variant needs a buffer,
which is allocated in the heap and managed by garbage collection.
result.setLength(0); result.append("value=").append(value);
The StringBuffer-Object can be allocated as an element of any class, the memory space is allocated
in the instance including the buffer itself, hence no dynamic memory allocation is necessary.
sprintf approach using String.format(formatString, arguments ...).
This method is some more safety as the C-sprintf, because a buffer overflow is detected and the types
of arguments are well known. But the method works with dynamic memory only.
StringBuffer is proper. The StringBuffer can be defined
as embedded Buffer with a fix length inside a class type. Examples using such a fix StringBuffer
allocated in the Stack too are test and shown in the method testStringBuffer().
| Field Summary | |
|---|---|
(package private) java.lang.StringBuilder |
buffer1
A reference to another struct. |
(package private) java.lang.StringBuilder |
bufferEmbedded
|
(package private) java.lang.StringBuffer |
bufferInit
A reference to another struct, but without need of garbage collection. |
private char[] |
charArray
|
(package private) static java.lang.String |
empty
|
(package private) java.io.FileOutputStream |
oStream1
|
(package private) java.io.FileOutputStream |
oStream2
|
private java.lang.StringBuffer |
sbufferFix
Creates an embedded instance as StringBuffer with immediate space for the chars. |
private java.lang.String[] |
strArray
|
private java.lang.String[] |
strArray2
|
(package private) java.lang.String |
stringRef
A reference to a String in memory. |
| Constructor Summary | |
|---|---|
TestString(java.lang.StringBuffer bufferInit)
|
|
| Method Summary | |
|---|---|
(package private) static int |
processString(java.lang.String str)
Help method which processes a String. |
private void |
testCharSequence()
A CharSequence is the super-class of java.lang.String and java.lang.StringBuilder. |
(package private) java.lang.String |
testDateString(java.lang.String sPath,
int ident)
This routine uses the class java.lang.Date und SimpleFormatter to produce a String with a date and time information. |
(package private) java.lang.String |
testDateStringDynamic(java.lang.String sPath,
int ident)
This routine uses the class java.lang.Date und SimpleFormatter to produce a String with a date and time information. |
private java.lang.String |
testFormat(int value,
float fValue)
|
(package private) void |
testGarbageString()
|
private void |
testInsertCharArray()
|
(package private) void |
testNonPersistenceOfStrings()
Persistence of Strings The method toString(...) |
(package private) void |
testOutStream(java.lang.String sPath)
|
(package private) void |
testPersistenceOfStrings()
This routine has the same content like testNonPersistenceOfStrings(),
but the persistence is set per default,
no @ java2c=optimize-toString. is written here. |
private void |
testReplace()
|
private int |
testSomeSimpleStringMethods()
|
(package private) void |
testStringBuffer()
Example for StringBuffer usage. |
(package private) void |
testStringConcatenationInStack(int value,
float fValue)
Examples for String concatenations. |
(package private) void |
testStringConcatenationUsingBuilder(int value)
|
(package private) void |
testStringConcatenationWithTemps(int value,
float fValue)
Examples for String concatenations. |
(package private) void |
testStringParameter()
|
(package private) java.lang.String |
testStringParameter(java.lang.String s1,
java.lang.String s2)
|
java.lang.String |
testStringProcessing()
Calls the test routines. |
java.lang.String |
toString()
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
java.lang.StringBuilder buffer1
final java.lang.StringBuffer bufferInit
final java.lang.StringBuilder bufferEmbedded
static final java.lang.String empty
java.lang.String stringRef
private final java.lang.StringBuffer sbufferFix
private final java.lang.String[] strArray
private java.lang.String[] strArray2
private char[] charArray
java.io.FileOutputStream oStream1
java.io.FileOutputStream oStream2
| Constructor Detail |
|---|
TestString(java.lang.StringBuffer bufferInit)
| Method Detail |
|---|
static int processString(java.lang.String str)
void testStringConcatenationInStack(int value,
float fValue)
void testStringConcatenationWithTemps(int value,
float fValue)
String ss = "test " + value + " miles";
System.out.println(ss);
this.stringRef = ss;
A String will be built stack-locally with concatenation. Than this String will be used
first as parameter for a method, than it is saved in the instance (persistent).
In C it is translated to:
StringJc ss;
...
StringBufferJc *_tempString1_1=null; //J2C: temporary Stringbuffer for String concatenation
...
ss =
( _tempString1_1 = new_StringBufferJc(-1, _thCxt)
, setTemporary_StringBufferJc(_tempString1_1)
, append_z_StringBufferJc(_tempString1_1, "test ", _thCxt)
, append_I_StringBufferJc(_tempString1_1, value, _thCxt)
, append_z_StringBufferJc(_tempString1_1, " miles", _thCxt)
, toString_StringBufferJc(_tempString1_1)
);
println_s_PrintStreamJc(REFJc(out_SystemJc), ss, _thCxt);
set_StringJc(&(ythis->stringRef), ss);
....
activateGarbageCollectorAccess_BlockHeapJc(&_tempString1_1->base.object);
The following principles are used:
new_StringBufferJc(...).
BlockHeap).
ss
uses the buffer.
append...(...)-methods.
toString(...) to get the String.
A String instance contains only the pointer to the text itself (char* in Buffer),
the number of chars and a few special bits. That are at maximum 2 Registers with 32 bit,
compendious in a struct StringJc and defined operation-system-depend
in a struct OS_ValuePtr to support optimal memory and register usage for the target platform.
StringJc*).
The method println(...) got the String as value.
setref_StringJc(...) is used.
This method sets the backward reference to the memory block of the block-heap, which contains
the pointer to the text and sets the index of backward reference in the reference in class (ythis->stringRef)
With this operation the block is referenced now.
void testStringConcatenationUsingBuilder(int value)
java.lang.String testStringParameter(java.lang.String s1,
java.lang.String s2)
void testStringParameter()
private java.lang.String testFormat(int value,
float fValue)
value - fValue -
private void testReplace()
private void testInsertCharArray()
public java.lang.String toString()
toString in class java.lang.Objectvoid testGarbageString()
void testStringBuffer()
bufferEmbedded is created as an embedded part of this class structure.
It has a fix length, the text is part of the instance, using immediate buffer.
The usage of such an buffer for string preparing is a proper decision if no dynamic memory
should be used, and the user has the controlling and responsibility about the String instances.
bufferEmbedded.setLength(0);
bufferEmbedded.append("content:").append(stringRef);
System.out.println(bufferEmbedded);
String s1 = bufferEmbedded.toString();
System.out.println(s1);
this.stringRef = s1;
In this example the buffer is cleared, than a content is appended. The Result is used to print out
with given StringBuffer. Than the buffer-content is stored in a String calling the toString(...)-method.
That result is used to print out too. At last the String is stored in a class variable.
StringJc s1;
...
setLength_StringBufferJc(& (ythis->bufferEmbedded.sb), 0, _thCxt);
( append_s_StringBufferJc(& (ythis->bufferEmbedded.sb), s0_StringJc("content:"), _thCxt)
, append_s_StringBufferJc(& (ythis->bufferEmbedded.sb), ythis->stringRef, _thCxt)
);
println_sb_PrintStreamJc(REFJc(out_SystemJc), & (ythis->bufferEmbedded.sb), _thCxt);
s1 = toString_StringBufferJc(& (ythis->bufferEmbedded.sb));
println_s_PrintStreamJc(REFJc(out_SystemJc), s1, _thCxt);
set_StringJc(&(ythis->stringRef), s1);
The C-code follows the Java-code. The concatenation of append is resolved like described in
TestAllConcepts.checkConcatenationSimple().
void testNonPersistenceOfStrings()
toString(...) needs to turn one's attentions: Generally, a String is constant.
Because the buffer content of a StringBuffer may be changed after calling toString(...),
the text have to be copied to an independent buffer. The Java implementation do so.
But this is an additional effort regarding typically applications in the fast real-time embedded world.
Mostly the StringBuffer isn't change immediately, rather the got String is processed and thereby copied
in the current thread like shown in the example testStringBuffer().
toString...(...) in the CRuntimeJavalike-Library have two modes:
toString...(...) creates an extra
buffer for the returned String in heap.
toString_StringBufferJc references the String in the
StringBuffer-instance, no extra buffer is used. toString...(...) from other classes
uses the thread-local Buffer, it can be used only one for one toString-result in thread, the result is to copy outside.
This mode saves calculation time and prevent an extra memory allocation.
In this case the String is dedicated with a bit mNonPersists__StringJc.
@ java2c=optimize-toString annotation
in its description block. Than the small-effort-mode is valid for that method body.
set_StringJc(...) checks whether the String is non persistent.
If it is so, a new StringBuffer is allocated in heap and the String is copied into. That StringBuffer
is only referenced by this String, and therefore it is fix and managed by garbage collection.
toString(...) implementations of some other Objects.
Because mostly the Strings will be processed immediately, at example as input for an concatenation,
extra buffers should be saved to minimize effort. For preparing the returned String a thread-local StringBuffer
is able to use. That is thread-save, but only one instance of that buffer exists per thread.
This buffer have never to be referenced. Thats why the same dedication is used: mNonPersists__StringJc.
The user can produce a faulty code, a code which runs in Java because Java is save but it doesn't run in C after translation.
This is the disadvantage of this concept, the concession to the optimizing of calculation time and preventing
additional dynamically memory management. The user may write minimal other statements to get a safety code.
@ java2c=optimize-toString., but the code is faulty for that:
bufferEmbedded.setLength(0);
String ss = bufferEmbedded.append("content:").append(234).toString();
bufferEmbedded.setLength(0);
bufferEmbedded.append(" TEST ").append(ss);
ss = bufferEmbedded.toString();
System.out.println(ss);
The buffer content is saved in ss, but after them the buffer is changed. The translated C-code is:
bool _bOptimizeStringSave; //J2C: annotation optimize-toString
...
_bOptimizeStringSave = optimizeString_ThCxt(_thCxt, true);
{
StringJc ss;
setLength_StringBufferJc(& (ythis->bufferEmbedded.sb), 0, _thCxt);
ss =
( append_s_StringBufferJc(& (ythis->bufferEmbedded.sb), s0_StringJc("content:"), _thCxt)
, append_i_StringBufferJc(& (ythis->bufferEmbedded.sb), 234, _thCxt)
, toString_StringBufferJc(& (ythis->bufferEmbedded.sb), _thCxt)
);
setLength_StringBufferJc(& (ythis->bufferEmbedded.sb), 0, _thCxt);
( append_s_StringBufferJc(& (ythis->bufferEmbedded.sb), s0_StringJc(" TEST "), _thCxt)
, append_s_StringBufferJc(& (ythis->bufferEmbedded.sb), ss, _thCxt)
);
ss = toString_StringBufferJc(& (ythis->bufferEmbedded.sb), _thCxt);
println_s_PrintStreamJc(REFJc(out_SystemJc), ss, _thCxt);
}
optimizeString_ThCxt(_thCxt, _bOptimizeStringSave);
The optimizing is set in the Thread-context-data. After the routine the saved state of optimizing
is recovered.
void testPersistenceOfStrings()
testNonPersistenceOfStrings(),
but the persistence is set per default,
no @ java2c=optimize-toString. is written here.
The result is correct. Debug it in C!
java.lang.String testDateString(java.lang.String sPath,
int ident)
java.lang.String testDateStringDynamic(java.lang.String sPath,
int ident)
testDateString(String, int),
but there are no annotations. Therefore dynamically memory is used.
void testOutStream(java.lang.String sPath)
private int testSomeSimpleStringMethods()
private void testCharSequence()
CharSequence csq1 = buffer1 is a simple downcast
from StringBuilder in Java, but in C it is a toString()-call. The text itself isn't copy
thereby, it is referenced. This is the equal functionality. Note that the StringJc is
a simple value-struct with reference and length.
public java.lang.String testStringProcessing()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||