|
a regexp like [\x00-\x09\x0B-\x0C\x0E-\x1F\x22\x3B\x3C\x3E\x7C] which is equivalent with if ( (ASCii <=9) || (ASCii == 11) || (ASCii == 12) || ( (ASCii >=14) && (ASCii <=31) ) || (ASCii == 34) || (ASCii == 59) || (ASCii == 60) || (ASCii == 62) || (ASCii == 124) )... hope i didn`t omit any of the ascii numbers or miscalculated their value
Code? Yeah i love it fried together with a glass of wine.
|
|
|
|
|
Looking at this gives me flashbacks...
You should think about using a regex util. I use 'The Regex Coach.' If it's a performance increase you should first consider rewriting the nasty looking conditional(s). Be aware that regular expressions can be a huge pain (and hurt performance) if done poorly.
Scott P
"Simplicity carried to the extreme becomes elegance."
-Jon Franklin
|
|
|
|
|
nesfrank wrote: this is too slow
nesfrank wrote: best performance
I think first of all you need to quantify what you mean here and have a way of timing a simple loop to cleanup the strings. I would have done much the same but in c++ I would have done something like :-
template <class T>
class is_garbage: public unary_function<char, bool>
{
public:
bool operator ()(T t) const
{
return ( !(isalpha( t ) || isdigit( t )));
}
};
std::string clean(const std::string& val)
{
string tmp = val;
string::iterator it0= remove_if(tmp.begin(), tmp.end(), is_garbage<char>());
string result(tmp.begin(),it0);
return result;
}
Which isnt that much different from what you've got, a bit more readable maybe
(NB : you would have to alter 'is_garbage' to match what you want, this is straight from my code, Ive made no attempt to replicate your issue)
Are you actually searching for strings within strings, replacing etc ? no ?? then I personally think a regex is an overkill ...
Ive copied this excerpt from John Maddock's page where he has the Regex++ library, although its been in Boost for a while now :-
"Regular expression libraries use a variety of differing algorithms all of which have their pro's and con's, which can make it hard to choose the best implementation for your purpose. This library uses an NFA algorithm which is dedicated to determining fast and accurate sub-expression matches, as well as providing support for features like back-references, which are hard to support using DFA algorithms.
People who should use this library:
Anyone who needs to use wide character strings.
Anyone who needs to search non-contiguous data.
Anyone who wants fast sub-expression matching.
Anyone who wants to customise the regular expression behaviour, or localise the library to a non-English locale.
People who should look to an alternative DFA based library:
Anyone who doesn't care about sub-expression matching, and
Wants to search only ANSI-C strings. " ref http://ourworld.compuserve.com/homepages/john_maddock/regexpp.htm[^]
'g'
|
|
|
|
|
I think you should forget about regular expressions as a solution to your problem. You really should consider cleaning up and re-working your code first... at least for the learning experience. Learning how to do these types of manipulations is pretty important and, looking at your code, you're just not there, yet.
All those temp variables and creating new strings in the loops area really expensive. Learn how strings work. Learn what immutable means and what happens when you build strings repeatedly within a loop.
As a first step, start from the basics. Learn to traverse a string and manipulate it character by character (as you attempted above). Start with something like this:
private string CleanString(string dirtyString)
{
StringBuilder cleanString = new StringBuilder();
foreach (char c in dirtyString)
{
if ((c != '\u0009') || (c != '\u000B') ... etc. )
{
cleanString.Append(c);
}
}
return (cleanString.ToString());
} Get that working, but then start using .NET's built in methods to improve your code.
Next, read about string.IndexOf(char) so you can search the entire string at once for a character. Rewrite your code and get that working.
Then, try creating an array of "dirty characters" so you can search for them all at once. Start by reading about this stuff:
char[] dirtyChars = new char[] { '\u0009', '\u000B', ... etc. };
int dirtyIndex = dirtyString.IndexOfAny(dirtyChars); Then rewrite your code again and get it working.
Then read about regular expressions, if you're curious. Will regular expressions work better? Maybe marginally... that's a really small "maybe." Probably not enough to matter. More readable?... I doubt it.
Enjoy,
Robert C. Cartaino
modified on Wednesday, November 19, 2008 5:24 PM
|
|
|
|
|
I found the asp.net answer, but it can't be applied to c#. Anyone know of a way to do this without creating a 2nd dataset to store it in?
|
|
|
|
|
|
You can hide those columns through the datagrid properties
|
|
|
|
|
H:\AllBrite\AllBrite\Form1.cs(3702): 'System.Windows.Forms.DataGrid' does not contain a definition for 'Columns'
using framework 1.1...
|
|
|
|
|
In our app am showing SQL exception being thrown to user (I know its not a good practice, but the scenario is different). It is always in English, any thoughts how to localize?
Thanks.
|
|
|
|
|
If you simply want SQL Server error messages shown with a different laguages, perhaps the easiest way is to use SET LANGUAGE (Transact-SQL)[^].
It also affects literal dateformat, but it can be changed after changing the language. In SQL Server 2005 and 2008 there are 33 different languages, refer to: sys.syslanguages (Transact-SQL)[^].
Mika
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
My context is , am catching he exceptions thrown from sql server in c# app and throwing as is. So, are you saying that to first launch "Set Language" statement from c# app before performing any SQL actions and that will throw localized exception messages?
|
|
|
|
|
Yes that's the idea. If I execute the following:
SET LANGUAGE Italian
SELECT * FROM NonExistingTable
The result is:
Msg 208, Level 16, State 1, Line 2
Il nome di oggetto 'NonExistingTable' non è valido.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
thanks Mika, i will try that.
|
|
|
|
|
You're welcome.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
I've only just started looking at Pointers and all that memory address changing and I want to be able to specify what address i want to change like 0023ff7c
Is this possible or have i completely got the wrong idea what pointers can do?
ta
|
|
|
|
|
That depends on what you think you can do with it. Yes, you can have a pointer look at any pipece of memory. No, you can't just poke anything you want, wherever you want. Also, from your applications point of view, it's the only app running on the machine, besides Windows. You can't just poke a byte into an arbitrary address and expect another application to see the change. It doesn't work like that.
|
|
|
|
|
thanks i don't realy get all this pointers buisness
i'll just give up and not do any more
|
|
|
|
|
Dave Kreskowiak wrote: poke
Ah, I remember the poke keyword on the old Commodore Basic, on the Apple ]['s as well. Used to always kind of snicker about it ( SB material creeping in )...
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Please help me again.
This is the weak dictionary i have created. It is compiling correctly. But when i test it with GC.Collect(), i can still refer to the weak reference values even after calling GC.Collect() method. How is it possible? What is wrong in my code.
namespace TryWeakDictionary{
public class WeakReference<T> : WeakReference {
public WeakReference(T val)
: base(val, false) {
}
}
public class WeakDictionary<TKey, TValue> : IDictionary<TKey,TValue> {
private Dictionary<TKey, WeakReference<TValue>> weakDictionary = new
Dictionary<TKey, WeakReference<TValue>>();
public void Add(TKey key, TValue value) {
WeakReference<TValue> weakRef = new WeakReference<TValue>(value);
this.weakDictionary.Add(key, weakRef);
}
public void Clear() {
this.weakDictionary.Clear();
}
public bool ContainsKey(TKey key) {
return this.weakDictionary.ContainsKey(key);
}
public void Remove(TKey key) {
this.weakDictionary.Remove(key);
}
public bool TryGetValue(TKey key, out TValue value) {
WeakReference<TValue> weakReference;
if (this.weakDictionary.TryGetValue(key, out weakReference) &&
weakReference.IsAlive) {
value = (TValue)weakReference.Target;
return true;
} else {
weakDictionary.Remove(key);
value = default(TValue);
return false;
}
}
}
}
|
|
|
|
|
This is the idea of weak references. Citing MSDN:
"A weak reference allows the garbage collector to collect an object while still allowing an application to access the object. If you need the object, you can still obtain a strong reference to it and prevent it from being collected."
Your code is OK.
EDIT: I have tested your code and it works as expected. How did you make youre tests?
Like this?
static void Main(string[] args)
{
TryWeakDictionary.WeakDictionary<int,> wd =
new TryWeakDictionary.WeakDictionary<int,>();
for (int i = 0; i < 100; i++) {
wd.Add(i, new C { Data = "abc" });
}
GC.Collect();
C v;
if (wd.TryGetValue(0, out v))
Console.WriteLine(v.Data);
Console.WriteLine("End");
Console.ReadLine();
}
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
modified on Tuesday, November 18, 2008 1:25 PM
|
|
|
|
|
There is nothing wrong in your code. Calling the GC.Collect method doesn't guarantee that all objects that can possibly be collected are collected. The garbage collector still decides what to collect and if there is any reason to do any collection at all.
You should shadow the Target property in the class, so that you don't have to cast it when you use it:
public class WeakReference<t> : WeakReference {
public WeakReference(T val) : base(val, false) {}
public new T Target { get { return (T)(base.Target); } }
}</t>
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Once again, thank you very much. Its all due to my misunderstanding that when i use GC.Collect, GC forcibly collects all objects....
|
|
|
|
|
I want to learn my computer name or others computer name. How can I do it? Could please you tell me how I can do it?
|
|
|
|
|
You can use Environment class.
|
|
|
|
|
I found it. the code is System.Environment.MachineName
|
|
|
|