ab.com コミュニティ

ActiveBasicを通したコミュニケーション
現在時刻 - 2024年3月28日(木) 20:42

全ての表示時間は UTC+09:00 です




新しいトピックを投稿する  トピックへ返信する  [ 2 件の記事 ] 
作成者 メッセージ
投稿記事Posted: 2014年6月20日(金) 03:51 
オフライン

登録日時: 2009年3月29日(日) 15:45
記事: 106
やっとの思いで完成しました。
文字列を置換するアルゴリズムです。

●使い方
コード:
置換された文字列 = SubstitutionText(置換対象文字列,置換したい文字列,置換する文字列)

buffer = SubstitutionText("aaaaabbbcccdddeeee","bbb","fffffff")

buffer の中身 = aaaaafffffffcccdddeeee
コード:
Function SubstitutionText(BaseData As BytePtr,SearchText As BytePtr,SubText As BytePtr) As BytePtr
	Dim StartPos As Long
	Dim EndPos As Long
	Dim Text As String
	Dim buffer As BytePtr
	Dim Length As Long
	Dim SubTextLen As Long

	StartPos = 1
	EndPos = 1
	buffer = NULL

	SubTextLen = lstrlen(SubText)

	Do
		'debug
		StartPos = InStr(StartPos,BaseData,MakeStr(SearchText))
		
		'開始文字がなければループを抜ける
		If StartPos <= 0 Then
			Text = Mid$(MakeStr(BaseData),EndPos)

			If buffer = NULL Then
				buffer = malloc(lstrlen(buffer)+lstrlen(Text) + 1)
				lstrcpy(buffer,Text)
			Else
				buffer = realloc(buffer,lstrlen(buffer)+lstrlen(Text) + 1)
				lstrcat(buffer,Text)
			End If

			Exit Do
		End If


		'スタートポジションまでを変数に格納
		If StartPos <> EndPos Then
			Text = Mid$(MakeStr(BaseData),EndPos,StartPos - EndPos)
		End If

		If buffer = NULL Then
			buffer = malloc(StartPos)
			lstrcpy(buffer,Text)
		Else
			buffer = realloc(buffer,lstrlen(buffer) + lstrlen(Text) + 1)
			lstrcat(buffer,Text)
		End If
		

		'置換処理
		buffer = realloc(buffer,lstrlen(buffer) + SubTextLen + 1)
		lstrcat(buffer,SubText)

		'終了ポジションを確定
		EndPos = StartPos + lstrlen(SearchText)

		'スタートポジションを進める
		StartPos = StartPos + lstrlen(SearchText)

	Loop

	SubstitutionText = buffer

End Function

_________________
↓個人的ソフト置き場
http://www.software.jpn.org/
↓萌えゲー製作とかしていたい
http://www.holygate.jpn.org/


通報する
ページトップ
投稿記事Posted: 2014年6月20日(金) 19:26 
オフライン

登録日時: 2006年2月05日(日) 17:10
記事: 215
お住まい: 東京都
早速利用させていただきました。
ちなみに置換された文字列の格納領域は呼び出し側で不要になり次第Free関数で解放することを記載したほうが良いと思います。
2015年7月12日修正


通報する
ページトップ
期間内表示:  ソート  
新しいトピックを投稿する  トピックへ返信する  [ 2 件の記事 ] 

全ての表示時間は UTC+09:00 です


オンラインデータ

このフォーラムを閲覧中のユーザー: なし & ゲスト[10人]


トピック投稿:  可
返信投稿:  可
記事編集: 不可
記事削除: 不可
ファイル添付: 不可

検索:
ページ移動:  
cron
Powered by phpBB® Forum Software © phpBB Limited
Japanese translation principally by ocean