<?xml version="1.0" encoding="UTF-8"?>
<codes type="array">
  <code>
    <code>private static void Log(object message, MessageType type)
		{
			string str = message.ToString();

			//Handle newlines
			if (str.IndexOf('\n') &gt;= 0)
			{
				foreach (string s in str.Split('\n'))
					Log(s, type);
				return;
			}

			
			//Split the string recursively in smaller strings to fit the console window
			for (int i = str.Length; i &gt; 0; i--) //TODO: Optimize, adding a message of 1000 characters takes too long
			{
				if (MessageFits(str.Substring(0, i)))
				{
					messages.Add(new Message(str.Substring(0, i), type));

					//If user was already all scrolled down, keep him scrolled down
					if (shownMessage == messages.Count - 2)
						shownMessage = messages.Count - 1;

					Log(str.Substring(i), type); //Recursive call to handle the rest

					break;
				}
			}

		}

		private static bool MessageFits(string message)
		{
			return font.MeasureString(message).X &lt;= window.ClientBounds.Width - (padding * 2) - scrollBarWidth;
		}</code>
    <comment>This is a console logging function. I'm trying to get the lines to split if they don't fit the display (checked with MessageFits, included). However, the code takes a good second to execute with a message of 1000 characters or more. Is there any way this could be faster?</comment>
    <created-at type="datetime">2009-12-07T20:59:43+00:00</created-at>
    <id type="integer">1122</id>
    <language>C#</language>
    <permalink>line-splitting-optimization</permalink>
    <refactors-count type="integer">3</refactors-count>
    <title>Line splitting optimization</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2010-01-19T18:40:42+00:00</updated-at>
    <user-id type="integer">1837</user-id>
    <user>
      <id type="integer">1837</id>
      <identity-url>http://xeon06.myopenid.com</identity-url>
      <name>xeon06.myopenid.com</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website nil="true"></website>
    </user>
  </code>
</codes>
